如何使用Delphi 10中的ISuperObject解析json文件

时间:2016-06-28 01:12:47

标签: json delphi

我尝试使用ISuperObject解析JSON文件。我的JSON文件如下所示:

{"status": "ok", "follows": {"count": 868, "page_info": {"has_previous_page": false, "start_cursor": null, "end_cursor": "SbXc6VJsoeTs", "has_next_page": true}, "nodes": [{"username": "username1", "requested_by_viewer": false, "followed_by_viewer": true, "profile_pic_url": "URL link", "full_name": "name", "is_verified": false, "id": "ID"}, {"username": "username2", "requested_by_viewer": false, "followed_by_viewer": true, "profile_pic_url": "URL link", "full_name": "username2", "is_verified": false, "id": "ID"}, {"username": "_username3", ...]}

我希望所有username值都列在备忘录中。

以下是我的尝试:

var
 json         : ISuperObject;
 row_item     : ISuperObject;
begin
  json := TSuperObject.ParseFile('C:\json.txt', TRUE); 
  for row_item in json['nodes'] do 
  begin
    Memo1.Lines.Add(elements_itemS['usernames']); 
  end;
end;

我在for循环上遇到了访问冲突。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

代码看起来应该更像这样:

var
  json         : ISuperObject;
  node         : ISuperObject;
  item         : IMember;
begin
  json := TSuperObject.ParseFile('C:\json.txt', TRUE); 
  for item in json.O['follows'].A['nodes'] do 
  begin
    node := item.AsObject;
    Memo1.Lines.Add(node.S['username']); 
  end;
end;

或者这个:

var
  json         : ISuperObject;
  node         : ISuperObject;
  item         : IMember;
begin
  json := TSuperObject.ParseFile('C:\json.txt', TRUE); 
  for item in json['follows.nodes'].AsArray do 
  begin
    node := item.AsObject;
    Memo1.Lines.Add(node.S['username']); 
  end;
end;