【问题标题】:How to parse json files using ISuperObject in Delphi 10如何在 Delphi 10 中使用 ISuperObject 解析 json 文件
【发布时间】:2016-06-28 01:12:47
【问题描述】:

我正在尝试使用 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 循环中遇到访问冲突。有什么想法吗?

【问题讨论】:

  • for username in json['nodes'] do through elements array 不是有效的 Delphi 代码,这意味着它不是您尝试过的。如果您需要有关代码的帮助,请发布您的代码。在我看来,我们之前已经进行过这样的讨论......
  • 我用了这个答案 [stackoverflow.com/questions/19415616/…,也许。 @KenWhite
  • 您链接的代码没有使用ISuperObject,它与您在此处发布的代码不同。我再说一遍:如果您需要代码方面的帮助,请发布您的代码。不是别的东西,不是不相关的东西的链接,不是您在此处写问题时编造的东西 - 您从 IDE 的代码编辑器复制和粘贴实际代码,这将实际编译。
  • 我使用了该帖子的第二个答案,并进行了一些更改,但是代码编译成功。更新。 @KenWhite
  • 非常令人失望的是您发布了无效的 JSON 和无法编译的代码。你真正的问题是缺乏对细节的关注。您需要学习如何准备minimal reproducible example

标签: json delphi


【解决方案1】:

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

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;

【讨论】:

  • json['nodes'].AsArray 导致此错误for-in statement cannot operate on collection type 'TSuperArray' because 'TSuperArray' does not contain a member for 'GetEnumerator', or it is inaccessible。删除 AsArray 会导致访问冲突!
  • 您使用的是latest version of ISuperObject吗? json['nodes'] 返回一个ISuperExpression,其AsArray 属性返回一个ISuperArray,而不是TSuperArrayjson.A['nodes'] 也返回一个ISuperArray)。 ISuperArray 有一个 GetEnumerator() 方法用于 Delphi for..in 循环。
  • 好的,更新了ISuperObject 现在我得到Incompatible types: 'ISuperObject' and 'ICast' 两种方式的答案
  • 枚举器的元素返回为ICast,所以将你的row_item变量改为ICast(或IMember,这是ICast的别名),然后你可以使用row_item.AsObject['username'].AsStringrow_item.AsObject.S['username']
  • 好的,现在可以编译了,但是没有添加到Memo1!,我打开调试器,点击事件日志中的Button1后没有添加任何进程
猜你喜欢
  • 2022-06-10
  • 2014-08-04
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多