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
} 
user3803565
  • 23
  • 1
  • 3

2 Answers2

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.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 1
    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 – user3803565 Jul 03 '14 at 23:55
  • 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. – Jerry Dodge Jul 03 '14 at 23:57
  • 2
    Instead of `O:= TSuperObject.ParseString(PChar(MyJsonString), False);` you can use `O:= SO(MyJsonString);` – Sir Rufo Jul 03 '14 at 23:59
  • @user3803565 The library contains a `readme.html` containing such basic examples https://code.google.com/p/superobject/source/browse/ – Sir Rufo Jul 04 '14 at 00:02
  • @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](http://meta.stackoverflow.com/q/257868/960757) at this time. – TLama Jul 04 '14 at 00:17
  • @TLama Indeed, exactly why I went ahead and wrote an answer – Jerry Dodge Jul 04 '14 at 00:47
  • My point is what is the harm in asking / answering such questions? It's not like anyone HAS to answer them and if you can help someone out "regardless of experience" then what harm have you done? I will never understand the elitist attitudes of some people. We all started out somewhere and if no question was ever asked then no answer would ever be given and thus nothing would ever be learned EVER. – user3803565 Jul 05 '14 at 15:49
  • @user3803565 It's more-so the ever evolving "rules and regulations" of Stack Overflow. – Jerry Dodge Jul 06 '14 at 00:59
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;
Feng Song
  • 1
  • 1
  • 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. – Taryn East Feb 12 '15 at 06:15
  • 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. – Feng Song Feb 12 '15 at 06:21
  • 1
    Don't tell me in the comments - please edit your answer and add the explanation... it should be part of your complete answer ;) – Taryn East Feb 12 '15 at 06:24