I have the JSON below:
{
"value": "[{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}]",
"formatters": [],
"contentTypes": [],
"declaredType": null,
"statusCode": 200
}
I need to get hold of value as an array using Delphi.
var
obj2: TJSonObject;
arr: TJSONArray;
The overall JSON is an object, so I first do:
obj2 := TJSONObject.ParseJSONValue(strJSon) as TJSONObject;
I then need to get the value part as an array, so I use:
arr := obj2.GetValue('value') as TJSONArray;
I get an Invalid Class Typecast error on the line above.
Does anyone know why?
valueis NOT an array (TJSONArray), it is a string instead (TJSONString), hence the cast error. Note the quote chars surrounding thevalue's data. If it were a true array, those quotes would not be present.valueis a string in the JSON, period. The JSON has no concept of what the string represents. You will have to parse the string yourself to make your own array from it. I suppose the simplest approach would be to retrieve the string and pass it to anotherParseJSONValue()call, and cast the resultingTJSONValuetoTJSONArray.valuesupposed to represent exactly?