Scroll through all json nodes

Asked

Viewed 920 times

0

I have the following Json file.

{"symbol":"KYCT","status":"ACTIVE"},{"symbol":"UNDA","status":"ACTIVE"},
{"symbol":"QUIA","status":"ACTIVE"}

I have the following reading routine that loads this file :

JsonValue := TJSONObject.ParseJSONValue(lResponse.DataString);

So far so good .

So I can get the values.

JsonValue.GetValue<string>('symbols[0].symbol')
JsonValue.GetValue<string>('symbols[0].status')

But it turns out the position [0], is the first position Symbol that’s in the file, and I’ll need to go through all the Symbols to get the property status, but I can’t imagine how I could go through this text aquivo, analyzing all of us.

I did some research but I couldn’t find anything, can help me please ?

3 answers

2


In the more modern versions of Delphi it is very simple to work with Json, but in smaller versions than XE8 it is complicated, a lot of gambiarra is necessary for a simple native use.

Native we can use something like:

var
  i: Integer;
  vMensagem: string;
  vJsonValue: TJSONArray;
begin
  vMensagem := '{"symbol":"KYCT","status":"ACTIVE"},{"symbol":"UNDA","status":"ACTIVE"},{"symbol":"QUIA","status":"ACTIVE"}';

  vJsonValue := TJSONObject.ParseJSONValue('[' + vMensagem + ']') as TJSONArray;

  for i := 0 to Pred(vJsonValue.Size) do
    ShowMessage(TJSONObject(vJsonValue.Get(i)).Get('symbol').JsonValue.Value);
end;

Note that we had changes, when consuming the object we had to convert it to an array.

vJsonValue := TJSONObject.ParseJSONValue('[' + vMensagem + ']') as TJSONArray;

Then it was enough to go through it accessing each of the objects:

vJsonValue.Get(i)

Here the .Get gives us back a JsonValue, then we are obliged to turn it into a JSONObject and access through the .Get that now is different and returns us a JsonPair. Knowing that it is a data pair we ask the value of the pair stating its name:

.Get('symbol').JsonValue.Value

On XE10 onwards the class Data.DBXJSON this discontinued, we now have a dedicated class System.Json much more complete and lean.

  • Thanks, I did it here and it all worked out !!!

  • 1

    Mark the answer as correct, so it helps in future research

1

Completing, if you have something like:

{"users":[{"name":"NOM1","code":1122334},{"name":"NOME2","code":1234}]}

To get the user values, in addition to the Junior Moreira response, adding brackets at the beginning and end of the json string and scrolling through the values, you can do:

if vJSONPair.JsonString.Value = 'users' then
   result := vJSONPair.ToString

However, the return of this will be:

"users":[{"name":"NOM1","code":1122334},{"name":"NOME2","code":1234}]

So I do:

copy(vJSONPair.ToString, 9, length(vJSONPair.ToString)- 8)

Because I know the return beforehand.

0

You can browse the JSON using a "for in" as shown below:

const
  JSON = '[{"symbol":"KYCT","status":"ACTIVE"},{"symbol":"UNDA","status":"ACTIVE"},{"symbol":"QUIA","status":"ACTIVE"}]';
var
  LJSONValue: TJSONValue;
  LJSONArray: TJSONArray;
begin
  LJSONArray := TJSONObject.ParseJSONValue(JSON) as TJSONArray;
  try
    for LJSONValue in LJSONArray do
      ShowMessage(LJSONValue.GetValue<string>('symbol'));
  finally
    LJSONArray.Free;
  end;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.