2

As the title suggest I am trying to extract a value from a very simply structured JSON file using Delphi 7 and the SuperObject Library, but I have yet to find any examples that cover this most basic topic and was hoping some of the gurus here might be able to offer me some assistance.

What I have is a simple JSON file (named test.json) that has the following structure and what I am wanting to know is how can I load this file in delphi and then extract the value for "last name" from the information provided.

I am sure this is an extremely simple task, but as I stated before I was not able to find any examples on how to do this and was hoping for some help.

example JSON file

{
  id: 212,
  first_name: "bob",
  last_name: "smith",
  age: 25
} 

2 Answers 2

3

First, declare an instance of the object, as an ISuperObject interface in this case. Then, assign it using TSuperObject.ParseString or even just SO to parse your JSON string. Then, you can read the values using the one-letter properties, depending on the type of value you're reading...

var
  O: ISuperObject;
  ID, Age: Integer;
  FirstName, LastName: String;
begin
  O:= SO(MyJsonString);
  ID:= O.I['id'];
  FirstName:= O.S['first_name'];
  LastName:= O.S['last_name'];
  Age:= O.I['age'];
end;

Please bear in mind however that things don't typically work this way here at Stack Overflow. The only reason I answered is because it was quick and easy, and because you appear to be new here. There are plenty of resources out there on how to use SuperObject; in the demos that you downloaded with the library, all over Google, and right here in Stack Overflow.

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

8 Comments

I did look at the demos and maybe I have an outdated version of the library, but I could see NOTHING that showed extraction of information and the same pretty much went for SO as well. I appreciate your reply however, not sure why any earnest question would be deemed unworthy however if one is seeking knowledge it should be offered without prejudice of experience (or lack there of) but that's just my $.02
Unfortunately, Stack Overflow is not here to help you learn. You are expected to make the effort to do that. When you get stuck on something in particular, you can ask specific questions. Otherwise, your question is very likely to draw negative attention.
Instead of O:= TSuperObject.ParseString(PChar(MyJsonString), False); you can use O:= SO(MyJsonString);
@user3803565 The library contains a readme.html containing such basic examples code.google.com/p/superobject/source/browse
@user3803565, well, the historical idea (which is far from true for a long time), was that Stack Overflow will be the site for professionals, site where the people ask after they do some research and show what they know so far. It wasn't meant to be the site where you come and say just "How to do this ?" and you get a solution. Besides, we don't even have any close vote to fight against them at this time.
|
0

The next is my example

function GetLastName(const FileName: string): string;
var
  O: ISuperObject;
begin
  // transport json file to superobject;
  O:= TSuperObject.ParseFile(FileName, False);
  // get value of object memeber 
  result:= O['last_name'].AsString;
end;

3 Comments

Hiya, this may well solve the problem... but it'd be good if you could edit your answer and provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them.
1. add "superobject" in uses statement, and add path of "superojbect.pas" in the option search path. 2. use TSuperObject.ParseFile() to transport json file to json object. 3. access elements in object via period accessor.
Don't tell me in the comments - please edit your answer and add the explanation... it should be part of your complete answer ;)

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.