Miguel E Miguel E - 4 months ago 8
JSON Question

Accessing inner JSON value

Having the following JSON structure:

concurrence: [
{
id: "481-13",
price: "11.5",
concurrent: {
id: 13,
name: "NAME1",
price: "11.5"
}
},
{
id: "481-14",
price: "12.25",
concurrent: {
id: 14,
name: "NAME2",
price: "12.25"
}
}
]


How can I get the "concurrent.id" value? I tried to use the "FindValue" function listed in the RAD Studio documentation, but it doesn't exist (at least in the 10.1 Berlin version). I'm accessing the concurrence array like this:

concurrents:= product.Get('concurrence').JSONValue as TJSONArray;
for j:= 0 to concurrents.Size-1 do
begin
concurrent:= concurrents.Get(j) as TJSONObject;
json_s:= concurrent.Get('id').JsonValue as TJSONString;
my_id:= json_s.Value;
json_s:= concurrent.Get('price').JsonValue as TJSONString;
my_price:= json_s.Value;
json_s:= concurrent.FindValue('concurrent.id') as TJSONString;//NOT WORKING
my_concurrent_id:= json_s.Value;
end;


Is there another simple way to do access the inner "concurrent" values?

Answer

You access items of concurrent the same way as every other JSON object.

You just have to get the JSON object itself before. But this is easy:

concurrent_sub:= concurrent.Get('concurrent').JsonValue as TJSONObject;
json_s := concurrent_sub.Get('id').JsonValue as TJSONString;