Recent

Author Topic: read a value from a simple json  (Read 491 times)

Hansvb

  • Hero Member
  • *****
  • Posts: 737
read a value from a simple json
« on: March 01, 2024, 04:44:34 pm »
Hi,

I have a very simple json. I want to read the value of ident_1. I have the following but it's not good.

json:
Code: Pascal  [Select][+][-]
  1. [
  2.   {
  3.     "ident_1" : "First",
  4.     "value_1" : 17
  5.   },
  6.   {
  7.     "ident_2" : "Second",
  8.     "value_2" : 3
  9.   }
  10. ]  


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   jData: TJSONData;
  4.   jArray: TJSonArray;
  5.   jObject : TJSONObject;
  6.   s: String;
  7.   i: Integer;
  8. begin
  9.   jData := GetJSON(memo1.text);
  10.   //jObject := TJSONObject(jData);
  11.  
  12.   jArray := TJSONArray(jData.FindPath('[0]'));  // finds both values (first and 17)
  13.   jArray := TJSONArray(jData.FindPath('[0].ident_1'));  // finds nothing, jArray =nil
  14. end;
  15.  

this is my help source: https://www.freepascal.org/docs-html/fcl/fpjson/tjsondata.findpath.html

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: read a value from a simple json
« Reply #1 on: March 01, 2024, 04:49:47 pm »
You have this:
Code: [Select]
TJSONArray(jData.FindPath('[0].ident_1')); But ident_1 isn't an array, but you cast the result to a TJSONArray  :o

Dzandaa

  • Sr. Member
  • ****
  • Posts: 407
  • From C# to Lazarus
Re: read a value from a simple json
« Reply #2 on: March 01, 2024, 05:16:40 pm »
Hi,

I use JSONTools from

http://www.getlazarus.org/json

Code: Pascal  [Select][+][-]
  1. procedure ReadJSON();
  2. var
  3.   N: TJsonNode;
  4.   id1, id2: String;
  5.   Val1, Val2: Double;
  6. begin
  7.   N := TJsonNode.Create;
  8.  
  9.   N.LoadFromFile('test.json');
  10.   if(N.Child(0).Exists('ident_1')) then id1 := N.Child(0).Find('ident_1').AsString;
  11.   if(N.Child(0).Exists('value_1')) then Val1 := N.Child(0).Find('value_1').AsNumber;
  12.   if(N.Child(1).Exists('ident_2')) then id2 := N.Child(1).Find('ident_2').AsString;
  13.   if(N.Child(1).Exists('value_2')) then Val2 := N.Child(1).Find('value_2').AsNumber;
  14.  
  15.   N.Free;
  16. end;  
  17.  

B->
« Last Edit: March 01, 2024, 06:06:00 pm by Dzandaa »
Regards,
Dzandaa

Hansvb

  • Hero Member
  • *****
  • Posts: 737
Re: read a value from a simple json
« Reply #3 on: March 01, 2024, 05:40:57 pm »
I allready had my answer when i Found both values. I just need to iterate through the array.  :-[
or do this:
Code: Pascal  [Select][+][-]
  1. s := jData.FindPath('[0].ident_1').AsString;

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: read a value from a simple json
« Reply #4 on: March 01, 2024, 05:48:47 pm »
I allready had my answer when i Found both values. I just need to iterate through the array.  :-[
or do this:
Code: Pascal  [Select][+][-]
  1. s := jData.FindPath('[0].ident_1').AsString;
No need to iterate.
But you need to do the correct casting.

Code: Pascal  [Select][+][-]
  1. s := TJSONData(jData.FindPath('[0].ident_1')).asString;

Although this might be better for error checking:
Code: Pascal  [Select][+][-]
  1. var
  2.   jNode: TJSONData;
  3. //...
  4.   jNode := jData.FindPath('[0].ident_1');
  5.   if assigned(jNode) then s := jNode.asString;

 

TinyPortal © 2005-2018