1

I want to parse this JSON using xSuperObject:

{
    "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
                    }
                }]
            }
        }
    }
}

Here is my code:

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;

I want to collect all username values from the JSON, but my code raises an AccessViolation.

1
  • 1
    Since I went through the extra effort of figuring out your JSON structure (since it was both unformatted and incomplete), I've edited it into your question so that nobody else has to. But please, provide things in a way that we can quickly test it in the future. Because now I've wasted my time and had to delete my answer over an unrelated typo. It's not fair when we have to put in more effort than you do. Commented Sep 8, 2017 at 23:41

1 Answer 1

4

You are attempting to jump straight to the edges array without first stepping through its parent elements. The actual location of the array you wish is at data.user.edge_followed_by.edges.

It should work more like this...

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.