简体   繁体   中英

Access JSON array using SuperObject

I have recently switched from using ULKJson to SuperObject and I have been looking around at the examples that come with the package and have made some headway with most it it, but it appears as I have come across a snag. To be more specific, I cannot seem to find an example to show how to access item in an array like the one in the example below.

{
  "name": "John Smith",
  "tel": 555-5555,
  "age": 18,
  "height": 1.8,
  "place": [{"address": "PO Box 1234", "city": "Florida", "code": 2000},
            {"address": "1 Sparrow street", "city": "Florida", "code": 2000}]
}

To access the regular items I use the following code which seems to work just fine.

procedure TForm1.Button1Click(Sender: TObject);
var
  SO : ISuperObject;
  age, height, tel : Integer;
  name : String;
begin
  SO := TSuperObject.ParseFile('JSON.txt',true);
  name := SO.S['name'];
  age := SO.I['age'];
  tel := SO.I['tel'];
  height := SO.I['height'];

  Memo1.Lines.Clear;

  Memo1.Lines.Add('Name: ' + name);
  Memo1.Lines.Add(#10#13);

  Memo1.Lines.Add('Age: ' + age);
  Memo1.Lines.Add(#10#13);

  Memo1.Lines.Add('Telephone: ' + tel);
  Memo1.Lines.Add(#10#13);

  Memo1.Lines.Add('Height: ' + height);
  Memo1.Lines.Add(#10#13);
end;

However, I am not sure how to access the items in the Place array and I am sure I am just overlooking something simple, but I could not find any examples in the demos which showed how to access this data and was hoping one of the gurus here might be able to offer some assistance or atleast point me to a guide where I can learn from myself.

The way I would do it would be simply:

var
  location:ISuperObject;
begin
   for location in SO['place'] do
      Memo1.Lines.Add(location.S['address']); //etc.
   end;
end;

And as TLama has suggested, the short guide really is a great source to learn from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM