Jump to content
Sign in to follow this  
Tim Koscielski

Parsing unknown JSON (into TW3StringGrid)

Recommended Posts

OK, I have looked at examples and I am sure I am just overlooking something simple.  I have a REST service returning simple results for various tables in a database.  The results can vary in the number of fields returned and the field names can be different. I know that I should be able to write a generic function to parse this JSON pattern, but I just can't seem to figure it out in SMS. I know I can do it with Javascript, but I am hoping someone can help fill in a blank I am missing in processing JSON with SMS. Below are two sample JSON text strings that I'd like to parse and add to a grid dynamically.

SAMPLE JSON ONE: [{"id":"1","name":"Joe"},{"id":"2","name":"Jill"},{"id":"3","name":"Sam"}]

SAMPLE JSON TWO: [{"id_person":"1","first":"Joe","last":"Smith"},{"id_person":"2","first":"Jill","last":"Jones"},{"id":"3","first":"Sam","last":"Butler"}]

I am solid on creating columns as needed, adding data to cells, etc. What I can't figure out is how to generically read each JSON string and determine what the fields are for each and then extracting the data for each item. Can anyone provide some help. Sometimes it is the easy things that get me. I have done this in Delphi without any issues in the past, but I need a little direction for SMS to shorten the learning curve.

Thanks.

Share this post


Link to post
Share on other sites

Have a look at the StringGrid -demo in Featured Demos. It downloads a JSON file, parses it and fills the grid.

Here's the relevant part:

    JsonData:=TJson.Parse(Sender.ResponseText); //Sender.ResponseText is the JSON as text

    W3StringGrid1.RowCount:=Length(JSonData); //Length returns how many records there are
    for i:=0 to W3StringGrid1.RowCount-1 do begin
      W3StringGrid1.Cells[i,0]:=JsonData[i].name;
      W3StringGrid1.Cells[i,1]:=JsonData[i].alpha3Code;
      W3StringGrid1.Cells[i,2]:=JsonData[i].capital;
      W3StringGrid1.Cells[i,3]:=JsonData[i].population;
    end;

JsonData is a simple Variant.

Share this post


Link to post
Share on other sites

Jarto, I did check this out. However, it is not generic for parsing JSON data. In that example the JSON file is defined in the code. In the DataReceive function, it has the items like 'name', 'alpha3Code', etc. already defined here. It is not generic and does show how one would go about determining if there are additional fields in the JSON dataset. Below is the code directly from that example. Ideally I'd liked to access those fields in a more generic fashion.
 

  try
    JsonData:=TJson.Parse(Sender.ResponseText);

    W3StringGrid1.RowCount:=Length(JSonData);
    for i:=0 to W3StringGrid1.RowCount-1 do begin
      W3StringGrid1.Cells[i,0]:=JsonData[i].name;
      W3StringGrid1.Cells[i,1]:=JsonData[i].alpha3Code;
      W3StringGrid1.Cells[i,2]:=JsonData[i].capital;
      W3StringGrid1.Cells[i,3]:=JsonData[i].population;
    end;
  finally
    Sender.Free;
  end;

 

Share this post


Link to post
Share on other sites
procedure TForm1.AnalyzeJSON(jsontxt: String);
var jroot: TJSONObject;
begin
  jroot:=TJSONObject.Create;
  jroot.FromJson(jsontxt);
  writeln('Items: '+IntToStr(jroot.Count));

  jroot.ForEach(function (Name: string; Data: variant): TEnumState
    begin
      var jobj:=TJSONObject.Create(Data);
      var k:=jobj.Keys;
      writeln('Keys: '+IntToStr(Length(k)));
      for var a:=0 to Length(k)-1 do begin
        var key:=k[a];
        Writeln(Format('Key %s has value %s',[key,jobj.Values[key]]));
      end;
      result:=esContinue;
    end);
end;

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...