如何使用 XsuperObject 从 JSON 响应中收集值

How To collect values from JSON response using XsuperObject

我想使用 xSuperObject 解析此 JSON:

{
    "data": {
        "user": {
            "edge_followed_by": {
                "count": 29594,
                "page_info": {
                    "has_next_page": true,
                    "end_cursor": ""
                },
                "edges": [{
                    "node": {
                        "id": "224289647",
                        "username": "h9a",
                        "full_name": "",
                        "profile_pic_url": "",
                        "is_verified": false,
                        "followed_by_viewer": false,
                        "requested_by_viewer": false
                    }
                }]
            }
        }
    }
}

这是我的代码:

var
  json : ISuperObject;
  item, item2 : IMember;
begin
  json := TSuperObject.Create(Memo1.Text);
  for item in json['edges'].AsArray do
  begin
    Memo2.Lines.Add(item.AsObject['node.username'].ToString);
  end;
end;

我想从 JSON 收集所有用户名值,但我的代码引发了 AccessViolation。

您正试图直接跳转到 edges 数组,而不首先单步执行其父元素。您想要的数组的实际位置是 data.user.edge_followed_by.edges

它应该更像这样工作...

var
  Obj: ISuperObject;
  Arr: ISuperArray;
  Itm: IMember;
begin
  Obj:= SO(Memo1.Lines.Text);
  Arr:= Obj.O['data'].O['user'].O['edge_followed_by'].A['edges'];
  for Itm in Arr do begin
    Memo2.Lines.Add(Itm.AsObject.O['node'].S['username']);
  end;
end;