0

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?

9
  • In the JSON you have shown, value is NOT an array (TJSONArray), it is a string instead (TJSONString), hence the cast error. Note the quote chars surrounding the value's data. If it were a true array, those quotes would not be present. Commented Oct 9, 2018 at 0:08
  • Ok that makes sense is there a best practice way to decide this so that it can become an array? Commented Oct 9, 2018 at 0:09
  • value is 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 another ParseJSONValue() call, and cast the resulting TJSONValue to TJSONArray. Commented Oct 9, 2018 at 0:10
  • Yuk this in reality could become a very big string with a lot of elements in it. The objects inside have a lot of properties Commented Oct 9, 2018 at 0:12
  • Why is JSON data being stringified at all? What is the value supposed to represent exactly? Commented Oct 9, 2018 at 0:18

2 Answers 2

1

You can apply this transformation to your StrJson variable, before doing the typecast

Uses StrUtils;

Var
strJson: String;

strJson:= ReplaceStr(strJson,'"[','[');
strJson:= ReplaceStr(strJson,']"',']');
strJson:= ReplaceStr(strJson,'\"','"');
vJSonArray:= TJSONObject.ParseJSONValue(strJson) as TJSONArray; //OK
Sign up to request clarification or add additional context in comments.

1 Comment

The format in which you get the JSON is not valid, or at least it is not what you expect. The text: "value": "[{\"field1\":\"value1\",\"field2\":\"value2\",\"field3\":\"value3\"}]" means you has created the entry with something like: ObjJson.AddPair(String, String) but, instead you should use: ObjJson.AddPair(String, TJSOnArray) The following example is valid, but the value is not an array, but a string MyValue:= '[{"field1":"Value1"}]'; ObjJson.AddPair('value', MyValue); This is the one you are looking for: ObjJson.AddPair(Texto, TJSONObject.ParseJSONValue(MyValue) as TJSONArray)
0

In the JSON you have shown, the value field is a string, not an array. That is why you are getting the cast error. Had it been an actual array, it would have looked like this instead:

"value": [{"field1":"value1","field2":"value2","field3":"value3"}],

Note the surrounding quotes are gone, and the inner quotes are not escaped as \".

If you know that the value string represents a JSON array, you can access the array like this:

var
  val1, val2: TJSONValue;
  obj: TJSONObject;
  arr: TJSONArray;
begin
  val1 := TJSONObject.ParseJSONValue(strJSon);
  try
    obj := val1 as TJSONObject;
    val2 := TJSONObject.ParseJSONValue(obj.GetValue('value').Value);
    try
      arr := val2 as TJSONArray;
      // use arr as needed ...
    finally
      val2.Free;
    end;
  finally
    val1.Free;
  end;
end;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.