Recent

Author Topic: [SOLVED] Process JSON structure  (Read 3333 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Process JSON structure
« on: December 04, 2017, 01:02:28 pm »
hi All,
i received a JSON format to extract the info from it (simplified structure below). it is not defined as an array although it has more than one object.
please advise what would be the best way to parse it with standard library (fpjson, jsonparser, jsonscanner).
thank you

Code: Pascal  [Select][+][-]
  1. {"A":{"field1":0, "field2": false},"B":{"field1":0, "field2": false}}
« Last Edit: December 04, 2017, 02:43:13 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: Process JSON structure
« Reply #1 on: December 04, 2017, 01:24:59 pm »
@tudi_x

If you go to LazMenu-->Tools-->Example Projects, you can find a project named "jsonviewer" with a generic parser(see attached image). You can feed your json and see what happens by debugging.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Process JSON structure
« Reply #2 on: December 04, 2017, 01:26:35 pm »
with all due respect GetMem i say we delete the wiki and point the users to the code.



« Last Edit: December 04, 2017, 01:42:24 pm by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: Process JSON structure
« Reply #3 on: December 04, 2017, 02:11:56 pm »
@tudi_x
Quote
with all due respect GetMem i say we delete the wiki and point the users to the code.
I'm not sure what you mean. Which wikipage are you referring to?

Just an idea to get started. I'm almost certain you can achieve the same thing with other methods too.
Code: Pascal  [Select][+][-]
  1. uses fpjson, jsonparser;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   Parser: TJSONParser;
  6.   Data: TJSONData;
  7.   JSON: TJSONStringType;
  8.   I: Integer;
  9.   DataName, ObjName, ObjValue: String;
  10. begin
  11.   JSON := '{"A":{"present":0},"B":{"present":1}}';
  12.   Parser := TJSONParser.Create(JSON);
  13.   try
  14.     Data := Parser.Parse;
  15.     try
  16.       if Data.JSONType = jtObject then
  17.       begin
  18.         for I := 0 to Data.Count - 1 do
  19.         begin
  20.           if Data.Items[I].JSONType = jtObject then
  21.           begin
  22.             DataName := TJSONObject(Data).Names[I];
  23.             ObjName := TJSONObject(Data.Items[I]).Names[0];
  24.             ObjValue := TJSONObject(Data.Items[I]).Get(ObjName);
  25.             ShowMessage(DataName + sLineBreak + '   ' + ObjName + sLineBreak + '        ' + ObjValue);
  26.           end;
  27.         end;
  28.       end;
  29.     finally
  30.       Data.Free;
  31.     end;
  32.   finally
  33.     Parser.Free;
  34.   end;
  35. end;

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Process JSON structure
« Reply #4 on: December 04, 2017, 02:31:25 pm »
thank you
the below worked for me:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DecodeString(AJSON: string);
  2. var
  3.   jsData: TJSONData;
  4.   jsItem: TJSONData;
  5.   i, j: word;
  6.   object_name, field_name, field_value, object_type, object_items: string;
  7.  
  8. begin
  9.   try
  10.     jsData := GetJSON(AJSON);
  11.  
  12.     for i := 0 to jsData.Count - 1 do
  13.     begin
  14.       jsItem := jsData.Items[i];
  15.  
  16.       object_type := GetEnumName(TypeInfo(TJSONtype), Ord(jsItem.JSONType));
  17.       object_name := TJSONObject(jsData).Names[i];
  18.       object_items := IntToStr(jsItem.Count);
  19.  
  20.       memo1.Append('object type: ' + object_type + '|object name: ' + object_name + '|number of fields: ' + object_items);
  21.  
  22.       for j := 0 to jsItem.Count - 1 do
  23.       begin
  24.         field_name := TJSONObject(jsItem).Names[j];
  25.         field_value := jsItem.FindPath(TJSONObject(jsItem).Names[j]).AsString;
  26.  
  27.         memo1.Append(field_name + '|' + field_value);
  28.       end;
  29.     end;
  30.  
  31.   finally
  32.     FreeAndNil(jsData);
  33.   end;
  34. end;    
Lazarus 2.0.2 64b on Debian LXDE 10

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Process JSON structure
« Reply #5 on: December 04, 2017, 02:42:59 pm »
i updated the wiki in http://wiki.freepascal.org/JSON#JSON_Object with a link to this code.
thank you!
Lazarus 2.0.2 64b on Debian LXDE 10

balazsszekely

  • Guest
Re: [SOLVED] Process JSON structure
« Reply #6 on: December 04, 2017, 02:53:51 pm »
Quote
i updated the wiki in http://wiki.freepascal.org/JSON#JSON_Object with a link to this code.
Thanks @tudi_x.

 

TinyPortal © 2005-2018