Recent

Author Topic: [ SOLVED ] Json parse problem  (Read 292 times)

superc

  • Full Member
  • ***
  • Posts: 235
[ SOLVED ] Json parse problem
« on: March 23, 2023, 08:58:26 am »
Hello,

I've a json like this:
Code: Pascal  [Select][+][-]
  1. {
  2.     "Input": [
  3.         {
  4.             "lang": {
  5.                 "bg": null,
  6.                 "cs": null,
  7.                 "da": null,
  8.                 "de": "asdasdasdasd",
  9.                 "el": null,
  10.                 "en": "asdasdasdasda",
  11.                 "es": "anmtrodsfsad",
  12.                 "et": null,
  13.                 "fr": "asdasdas",
  14.                 "hr": null,
  15.                 "hu": null,
  16.                 "it": "asda grill",
  17.                 "lt": null,
  18.                 "lv": null,
  19.                 "nl": null,
  20.                 "pl": "asdasdasd grilla",
  21.                 "pt": "asdasdasda",
  22.                 "ro": null,
  23.                 "ru": "\u0441\u0435\u043d\u0441\u043e\u0440\u043d\u044b\u0439 \u0433\u0440\u0438\u043b\u044c",
  24.                 "sk": null,
  25.                 "sl": null,
  26.                 "sr": null,
  27.                 "sv": null,
  28.                 "tr": null
  29.             },
  30.             "val_par": 1
  31.         }
  32.     ]
  33. }
  34.  

I wrote a code like this :
Code: Pascal  [Select][+][-]
  1. var
  2.   jInput,jVersions: TJSONArray;
  3.   js,
  4.   o,t:TJSONData;
  5.   str: string;
  6.   i,c: Integer;
  7.   JsonFile: TFileStream;
  8.   fileStream: TFileStream;
  9.   tl :TStringList;
  10.   stTemp : string;
  11. begin
  12.   try
  13.     tl := TStringList.Create;
  14.     tl.LoadFromFile(fileJson);
  15.     js := GetJSON(tl.Text);
  16.     o := js.FindPath('Input');
  17.  
  18.     if (o <> nil) and (o.JSONType = jtArray) then
  19.     begin
  20.       jInput := TJSONArray(o);
  21.  
  22.       for i := 0 to jInput.Count-1 do
  23.       begin
  24.         o := jInput[i].FindPath('lang');
  25.         if (o <> nil) and (o.JSONType = jtObject) then
  26.         begin
  27.  
  28.         end;
  29.       end;
  30.  
  31.     end;
  32.   finally
  33.     js.Free;
  34.     tl.Free;
  35.   end;
  36. end;    
  37.  

I don't understand how to reach language tags like "bg", "cs", etc...

Thanks in advance.
« Last Edit: March 23, 2023, 11:58:50 am by superc »

superc

  • Full Member
  • ***
  • Posts: 235
Re: Json parse problem
« Reply #1 on: March 23, 2023, 09:05:07 am »
found it :
Code: Pascal  [Select][+][-]
  1.         o := jInput[i].FindPath('lang');
  2.         if (o <> nil) and (o.JSONType = jtObject) then
  3.         begin
  4.           stTemp := o.FindPath('de').Value;
  5.         end;    
  6.  



loaded

  • Hero Member
  • *****
  • Posts: 757
Re: Json parse problem
« Reply #2 on: March 23, 2023, 09:12:05 am »
Code: Pascal  [Select][+][-]
  1. uses fpjson;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. var
  5.   jmain:TJSONData;
  6. begin
  7.   jmain:=GetJSON('{"Input":[{"lang":{"bg":null,"cs":null,"da":null,"de":"asdasdasdasd","el":null,"en":"asdasdasdasda","es":"anmtrodsfsad","et":null,"fr":"asdasdas","hr":null,"hu":null,"it":"asda grill","lt":null,"lv":null,"nl":null,"pl":"asdasdasd grilla","pt":"asdasdasda","ro":null,"ru":"сенсорный гриль","sk":null,"sl":null,"sr":null,"sv":null,"tr":null},"val_par":1}]}');
  8.   showmessage(jmain.FindPath('Input').items[0].FindPath('lang').FindPath('de').AsString);
  9. end;
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

superc

  • Full Member
  • ***
  • Posts: 235
Re: Json parse problem
« Reply #3 on: March 23, 2023, 11:58:37 am »
thanks, your code will help me for another function.


 

TinyPortal © 2005-2018