Home > Mobile >  Create an Array from Json Object DELPHI
Create an Array from Json Object DELPHI

Time:11-13

I have the following JSON structure, calling it from web:

[
  {
    "Code":"31212",
    "Desc":"JOHN",
    "DOYDESC":"MADRID",
    "Street":"Str41",
    "StreetNo":"86"
  },
  {
    "Code":"30214",
    "Desc":"GEORGE",
    "DOYDESC":"NEW YORK",
    "Street":"Str3",
    "StreetNo":null
  },
  {
    "Code":"09215",
    "Desc":"MARY",
    "DOYDESC":"PARIS",
    "Street":"Str3",
    "StreetNo":"22"
  },
  {
    "Code":"10217",
    "Desc":"MEGAN",
    "DOYDESC":"ROME",
    "Street":"Str4",
    "StreetNo":null
  }
]

I want the user to give a number in Edit.Text, and then search the values of Code to find if the given value exists.

Is there a way to create an array named Code with the values from the JSON?

For example, to create something like this:

Code = ["31212","30214","09215","10217"] 

and then make the search? Or, is there another way to find a specific value of Code from the JSON? I tried this so far:

procedure Tlog_form.Button2Click(Sender: TObject);
var
  jso : TJsonObject;
  jsv,jsval : TJsonValue;
  jsa,jsarr : TJsonArray;
  data,str : string;
  i,j,user : integer;
  jsp : TJsonPair;
  arr : array of string;
begin
  try
    data := GetURLAsString('http://...');
  except
    on E: exception do
  end;

  try
    jsv := TJSONObject.ParseJSONValue(data);

    try
      jsa := jsv as TJSONArray;

      try
        for I := 0 to jsa.Size - 1 do
        begin
          jso := jsa.Get(i) as TJsonObject;
          if jso.ToString = 'Code' then
          begin
            str := jso.GetValue('Code').ToString;

            if Edit1.Text = str then
            begin
              ShowMessage('found it');
            end;

          end;
        end;
      finally
      end;
    finally
      jsv.Free;
    end;
  except
    on E: exception do
  end;
end;

When I debug, it doesn't get any error. but it still doesn't work.

CodePudding user response:

You are converting each TJSONObject in the array to a string, and then comparing that to your TEdit text. Which will obviously not match. You need to compare only the Code field of each TJSONObject, which you would be doing if you remove the if jso.ToString = 'Code' then check completely, eg:

procedure Tlog_form.Button2Click(Sender: TObject);
var
  jsv : TJSONValue;
  jsa : TJSONArray;
  jso : TJSONObject;
  data, str, code : string;
  I : integer;
begin
  str := Edit1.Text;
  try
    data := GetURLAsString('http://...');
    jsv := TJSONObject.ParseJSONValue(data);
    if jsv <> nil then
    try
      jsa := jsv as TJSONArray;
      for I := 0 to jsa.Size - 1 do
      begin
        jso := jsa.Get(I) as TJSONObject;
        code := jso.GetValue('Code').ToString;
        if str = code then
        begin
          ShowMessage('found it');
        end;
      end;
    finally
      jsv.Free;
    end;
  except
  end;
end;

CodePudding user response:

I'm not sure I understand you very well, but if you try TALJsonDocument (https://github.com/Zeus64/alcinoe), you have this function:

Function ALFindJsonNodeByTextChildNodeValue(const JsonNode:TalJsonNode;
                                            Const ChildNodeName: AnsiString;
                                            Const ChildNodeValue : AnsiString;
                                            Const Recurse: Boolean = False): TALJsonNode;
  • Related