[SOLVED] Json parse problem lazarus / 21-General ---------------------------------------------------------------------- superc · 2022-02-08 10:35 Hello, I've to parse a json like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {  "Email": "bs@zz.top",  "CustomerId": "007",  "products" : [    {        "Code": "loremipsum",        "Version": [            {                "v": "1",                "FileName": "1D5A8825C33AD8B88C36DA1B3CC19C9A.zip",                "Validate": "1",                "Hash": "1D5A8825C33AD8B88C36DA1B3CC19C9A"          },            {                "v": "2",                "FileName": "2D343E4F01F28C426AF37EC673C4BFDE.zip",                "Validate": "1",                "Hash": "2D343E4F01F28C426AF37EC673C4BFDE"          },            {                "v": "3",                "FileName": "1ACCC033EF31DDF5297951EBB27458A8.zip",                "Validate": "0",                "Hash": "1ACCC033EF31DDF5297951EBB27458A8"          },            {                "v": "4",                "FileName": "APPAACCAAPPAPPAPAPAPAPAPAPAAPAPP.zip",                "Validate": "0",                "Hash": "APPAACCAAPPAPPAPAPAPAPAPAPAAPAPP"          },            {                "v": "5",                "FileName": "ANNANANANANANANAANANANANANANNAAN.zip",                "Validate": "0",                "Hash": "ANNANANANANANANAANANANANANANNAAN"          },            {                "v": "6",                "FileName": "TARASDASDASDASDASDASDARDQCVCDFVV.zip",                "Validate": "0",                "Hash": "TARASDASDASDASDASDASDARDQCVCDFVV"          }        ]      },    {        "Code": "loremipsum1",        "Version": [            {                "v": "1",                "FileName": "1dd0e5bfc954bc46fe800952be7e2a69.zip",                "Validate": "1",                "Hash": "1dd0e5bfc954bc46fe800952be7e2a69"            },            {              "v": "2",                "FileName": "d1332e54c7fbb49e96e7186dc9e3bf1a.zip",                "Validate": "1",                "Hash": "d1332e54c7fbb49e96e7186dc9e3bf1a"          }        ]      },        {        "Code": "loremipsum2",        "Version": [            {                "v": "4",                "FileName": "asdasdasdasdasdasdasdasdasdasada.zip",                "Validate": "1",                "Hash":         "asdasdasdasdasdasdasdasdasdasada"            },            {              "v": "5",                "FileName": "ertertertertertertertertertertete.zip",                "Validate": "1",                "Hash":         "ertertertertertertertertertertete"            }        ]      },      {        "Code": "loremipsum3",        "Version": [            {              "v": "1",                "FileName": "12312312312312312312312312321312.zip",                "Validate": "1",                "Hash":         "12312312312312312312312312321312"            }        ]      }         ]} I start to write a code like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var   ja: TJSONArray;  js,  o:TJSONData;  jo: TJsonObject;  a : array of TData;  str: string;  i,c: Integer;begintry  str := Memo1.Lines.Text;  js := GetJSON(str);  o := js.FindPath('products');   if (o <> nil) and (o.JSONType = jtArray) then  begin    ja  := TJSONArray(o);    SetLength(a, ja.Count);     for i := 0 to ja.Count -1 do    begin    o := ja.Objects[i].FindPath('Version');       if (o<>nil) and (o.JSONType = jtArray) then      begin        ja  := TJSONArray(o);      SetLength(a, ja.Count);         for c := 0 to ja.Count -1 do      begin          o := ja.Objects[c].FindPath('FileName');          jo := TJSONObject(o);         end;       end;    end;  end;  finally  js.Free;end;        I have to go through the whole document and obtain for every Version all information likes "v", "filename", "validate", "hash" but receive SIGSEGV when casting jo: = TJSONObject (o); obviously I'm wrong something, but I don't understand what: how do i get the desired result? Thanks in advance. ---------------------------------------------------------------------- loaded · 2022-02-08 11:14 An application that parses the sample json data you provide with the experience I gained while doing folder browsing work; I would like to take this opportunity to thank everyone who has a written line code in the forum. ---------------------------------------------------------------------- superc · 2022-02-08 11:17 with this code I was able to get all value of first section "loremipsum": Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var   ja: TJSONArray;  js,  o:TJSONData;  jo: TJsonObject;  a : array of TData;  str: string;  i,c: Integer;begintry  str := Memo1.Lines.Text;  js := GetJSON(str);  o := js.FindPath('products');   if (o <> nil) and (o.JSONType = jtArray) then  begin    ja  := TJSONArray(o);    SetLength(a, ja.Count);     for i := 0 to ja.Count -1 do    begin    o := ja.Objects[i].FindPath('Code');       if (o <> nil) and (o.JSONType = jtString) then        Memo2.Lines.Add(o.Value);        o := ja.Objects[i].FindPath('Version');      if (o<>nil) and (o.JSONType = jtArray) then      begin        ja  := TJSONArray(o);        SetLength(a, ja.Count);         for c := 0 to ja.Count -1 do        begin          o := ja.Objects[c].FindPath('FileName');          if (o <> nil) and (o.JSONType = jtString) then            Memo2.Lines.Add(o.Value);        end;      end;    end;  end;finally  js.Free;end;     now the problem is that only the first element "loremipsum" seems to be cycled correctly... I do not understand why ---------------------------------------------------------------------- engkin · 2022-02-08 15:05 Maybe this makes it a bit clear: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnParseClick(Sender: TObject);var  jProducts,jVersions: TJSONArray;  js,  o,t:TJSONData;  str: string;  i,c: Integer;begin  try    str := Memo1.Lines.Text;    js := GetJSON(str);    o := js.FindPath('products');     if (o <> nil) and (o.JSONType = jtArray) then    begin      jProducts := TJSONArray(o);      memo2.Append('products.count: '+jProducts.Count.ToString);      for i := 0 to jProducts.Count-1 do      begin        o := jProducts[i].FindPath('Code');        if (o <> nil) and (o.JSONType = jtString) then          Memo2.Append(Format('products[%d].Code: %s',[i,o.AsString]));         t := jProducts[i].FindPath('Version');      if (t<>nil) and (t.JSONType = jtArray) then        begin        jVersions  := TJSONArray(t);          memo2.Append(Format('product[%d].Version.count: %d',[i,jVersions.Count]));           for c := 0 to jVersions.Count -1 do          begin            t := jVersions[c].FindPath('FileName');          if (t <> nil) and (t.JSONType = jtString) then              Memo2.Append(Format('product[%d].Version[%d].FileName: %s',[i,c,t.AsString]));          end;          Memo2.Append('');      end;      end;    end;  finally    js.Free;  end;end; ---------------------------------------------------------------------- superc · 2022-02-08 15:34 Quote from: engkin on February 08, 2022, 04:05:06 pmMaybe this makes it a bit clear: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnParseClick(Sender: TObject);var  jProducts,jVersions: TJSONArray;  js,  o,t:TJSONData;  str: string;  i,c: Integer;begin  try    str := Memo1.Lines.Text;    js := GetJSON(str);    o := js.FindPath('products');     if (o <> nil) and (o.JSONType = jtArray) then    begin      jProducts := TJSONArray(o);      memo2.Append('products.count: '+jProducts.Count.ToString);      for i := 0 to jProducts.Count-1 do      begin        o := jProducts[i].FindPath('Code');        if (o <> nil) and (o.JSONType = jtString) then          Memo2.Append(Format('products[%d].Code: %s',[i,o.AsString]));         t := jProducts[i].FindPath('Version');      if (t<>nil) and (t.JSONType = jtArray) then        begin        jVersions  := TJSONArray(t);          memo2.Append(Format('product[%d].Version.count: %d',[i,jVersions.Count]));           for c := 0 to jVersions.Count -1 do          begin            t := jVersions[c].FindPath('FileName');          if (t <> nil) and (t.JSONType = jtString) then              Memo2.Append(Format('product[%d].Version[%d].FileName: %s',[i,c,t.AsString]));          end;          Memo2.Append('');      end;      end;    end;  finally    js.Free;  end;end; Yes, thank you very much, my error is have used  the same tjsondata for product and version.