I want to check if the JSON object has the key "error"
. If it has, I want to "continue" the loop, otherwise the program can go to all the loop.
This is the JSON:
[
{
"cenario": {
"origem": "",
"out": "SNC",
"country": "",
},
"item": "0015963",
"cod": "17894904009319",
"nat_rec": null
},
{
"item": "0012868",
"error": "product unavailable",
"status": "unavailable",
}
]
How can I check if the object that I’m reading has the key "error"
or not?
I tried:
jValue.FindValue('error') // The problem it's going to search for all objects.
jValue.TryGetValue('error', jArray) // if it doesn't find the key in the index that it's searching at the moment, it breaks the application.
I’m doing:
response:= IdHTTP.Get(url);
jValue:= TJsonObject.ParseJSONValue(response);
for x := 0 to 2 do
begin
if jValue.TryGetValue('error', jvalue) then
begin
continue;
end;
memo.Lines.Add('cod_item :' + jValue.GetValue<string>('['+intToStr(x)+'].item'));
memo.Lines.Add('cod: ' + jValue.GetValue<string>('['+intToStr(x)+'].cod'));
end;
2
Answers
You can parse it to an array and iterate the array. The two functions HasValueAtObjectKey & GetValueAtObjectKey demonstrate two different ways of checking the object for the key.
In your code,
jValue
is pointing at aTJSONArray
, so when you callTryGetValue('error')
on it, you are looking for a field named'error'
on the array itself, which obviously is not going to work. That would only work when called on aTJONObject
instead.You would need to include each object’s index when querying for the
'error'
field on the array itself, eg:Alternatively, you can iterate the actual objects in memory instead of searching via paths, eg: