3

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.

avue
  • 129
  • 1
  • 6
  • 15
  • The guide is of little help when the example provided will not compile. – avue Jul 06 '14 at 02:41
  • It's not a complete code. It's a set of examples showing how to do basic things. It's not supposed to compile. Besides, *"will not compile"* tells absolutely nothing about what you wrote. – TLama Jul 06 '14 at 07:14
  • Was that you who flagged my initial comment for deletion ? If so, why ? Because that [`set of examples`](https://superobject.googlecode.com/git/readme.html) doesn't compile for you or something ? – TLama Jul 07 '14 at 12:52
  • Yes, I flagged the reply for not really adding to the question. Far to often people on SO use the RTFM reply and quite simply put, it is rather assumptive to suggest the OP did not already try to figure out the issue one his/her own by doing just that in the first place, especially when they have provided an example which clearly shows they have at least a basic understanding of the material in question. The fact that the examples did not work in my case had little to do with it so to answer your question.. yes and no. – avue Jul 07 '14 at 16:55
  • Well, I've tried to point you to a set of examples that you clearly missed. And you didn't show any attempt to work with arrays. You've shown just what you know asking for the rest. Besides your "never mind" reaction below is pointless. Instead you should post your own answer. – TLama Jul 07 '14 at 17:36

1 Answers1

7

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 for .. in enum is only available in D2005 and up. I am using Delphi 7 so what would the equivalent code be? – avue Jul 06 '14 at 03:02
  • Nvm, I managed to figure it out using low() and high() which makes things look ugly but it works. – avue Jul 06 '14 at 03:26