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.

5
  • The guide is of little help when the example provided will not compile. Commented Jul 6, 2014 at 2: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. Commented Jul 6, 2014 at 7:14
  • Was that you who flagged my initial comment for deletion ? If so, why ? Because that set of examples doesn't compile for you or something ? Commented Jul 7, 2014 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. Commented Jul 7, 2014 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. Commented Jul 7, 2014 at 17:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The for .. in enum is only available in D2005 and up. I am using Delphi 7 so what would the equivalent code be?
Nvm, I managed to figure it out using low() and high() which makes things look ugly but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.