ahausladen / JsonDataObjects

JSON parser for Delphi 2009 and newer
MIT License
413 stars 160 forks source link

How to get each item(it's Object) from the ARRAY? #20

Closed chonghai closed 8 years ago

chonghai commented 8 years ago

How to get each item(Object, not a single Value) from the ARRAY? thks!

chonghai commented 8 years ago

just like this question: http://stackoverflow.com/questions/33127718/how-can-i-access-a-sub-object-in-jsondataobjects

ahausladen commented 8 years ago

You can iterate through the array or you can use the Path[] property.

procedure Test(AData: TJsonObject);
var
  Obj: TJsonObject;
begin
  Obj := AData.Path['MyArray[1].MySubObject'];
  ...
end;
chonghai commented 8 years ago

thks for your answer. but I test it , it seems not work. here is some JSON string:

{"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "Help"}, {"id": "About", "label": "About Adobe CVG Viewer..."} ] }}

How can I Get "Help" in {"id": "Help"}? Thks!

ahausladen commented 8 years ago
if Json.Path['menu.items[3].id'] <> 'Help' then
  raise Exception.Create('Something is wrong');
chonghai commented 8 years ago

sorry, I make mistake, I user a wrong JSON string. Your code is good.

another question: How to use iterate through the array to show each "id"? like:

for jsonitem in obj['menu'].['items'] do
  showMessage(jsonitem['id']);
ahausladen commented 8 years ago

"for in" is not supported. You have to iterate manually using the array's Count property.

chonghai commented 8 years ago

OK, thanks a lot of!