SuperObject

编程入门 行业动态 更新时间:2024-10-27 08:27:18
SuperObject - 全部提取(SuperObject - Extract All)

如何从通用JSON获取所有'id'成员值不知道它的结构 。 因为它非常复杂并且有很多子对象。 它必须循环遍历所有子对象。

对于继续询问示例JSON在哪里的人来说也是如此。 我的问题是如何在我的案例“id”中从任何具有此成员的通用JSON中提取成员值

How to get ALL 'id' member values from a generic JSON. Without knowing structure of it. Because its very complex and it has a lot of sub objects. It has to loop through all the sub objects.

Again for people that keep on asking where is the example JSON. My question is about how to extract a member value in my case "id" from any generic JSON that has this member inside.

最满意答案

如果你不知道从某个地方收到的JSON的结构,重要的是要注意JSON“简单地”是一个复合模式,你可以像任何其他复合结构一样遍历它。 以下示例遍历JSON文本中的完整结构,并打印任何名为'id'的成员的路径。

procedure ParseJSON; var JSONText: string; JSON: ISuperObject; begin // Retrieve JSON as a string into JSONText variable any way you like. JSON := SO(JSONText); ProcessObject(JSON.AsObject); end; procedure ProcessObject(const aAsObject: TSuperTableString; const aPrefix: string = ''); var Names: ISuperObject; Name: string; Items: ISuperObject; Item: ISuperObject; idx: Integer; Value: string; ArrayItem: ISuperObject; begin if Assigned(aAsObject) then begin Names := aAsObject.GetNames; Items := aAsObject.GetValues; for idx := 0 to Items.AsArray.Length - 1 do begin Name := Names.AsArray[idx].AsString; Item := Items.AsArray[idx]; if Item.DataType = stObject then Value := '<Object>' else if Item.DataType = stArray then Value := '<Array>' else Value := Item.AsString; if SameText(Name, 'id') then WriteLn(Format('%s: %s', [aPrefix + Name, Value])); if Item.DataType = stArray then for ArrayItem in Item do ProcessObject(ArrayItem.AsObject, aPrefix + Name + '.'); if Item.DataType = stObject then ProcessObject(Item.AsObject, aPrefix + Name + '.'); end; end; end;

If you don't know the structure of the JSON you receive from somewhere, it is important to note that JSON is "simply" a composite pattern and you can traverse it like any other composite structure. The following example traverse the complete structure in a JSON text and prints the path of any member named 'id'.

procedure ParseJSON; var JSONText: string; JSON: ISuperObject; begin // Retrieve JSON as a string into JSONText variable any way you like. JSON := SO(JSONText); ProcessObject(JSON.AsObject); end; procedure ProcessObject(const aAsObject: TSuperTableString; const aPrefix: string = ''); var Names: ISuperObject; Name: string; Items: ISuperObject; Item: ISuperObject; idx: Integer; Value: string; ArrayItem: ISuperObject; begin if Assigned(aAsObject) then begin Names := aAsObject.GetNames; Items := aAsObject.GetValues; for idx := 0 to Items.AsArray.Length - 1 do begin Name := Names.AsArray[idx].AsString; Item := Items.AsArray[idx]; if Item.DataType = stObject then Value := '<Object>' else if Item.DataType = stArray then Value := '<Array>' else Value := Item.AsString; if SameText(Name, 'id') then WriteLn(Format('%s: %s', [aPrefix + Name, Value])); if Item.DataType = stArray then for ArrayItem in Item do ProcessObject(ArrayItem.AsObject, aPrefix + Name + '.'); if Item.DataType = stObject then ProcessObject(Item.AsObject, aPrefix + Name + '.'); end; end; end;

更多推荐

本文发布于:2023-07-30 23:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SuperObject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!