0

I have a problem to parse an array in Json. The Json is like this example:

    [
      [
        A1,
        A2,
        A3,
      ],
     [
        B1,
        B2,
        B3,
      ],

and so on.

I get always an access violation error if I try to parse it:

procedure tform1.test;
var
 i:integer
 value,A:string;
 jValue:TJSONValue;
 JSonValue:TJSonValue;
 Jarray:TJSONArray;

 begin
 jValue:=RESTResponse1.JSONValue;
 Jarray := TJSonObject.ParseJSONValue(value) as tjsonarray;
 for i := 0 to Jarray.Count - 1 do
 A:=Jarray.items[i].value;
 end;

What am I doing wrong?

1
  • 2
    Use your debugger and find out exactly where the error is. Commented Nov 24, 2019 at 22:06

1 Answer 1

2

Use JsonValue as TJSONArray twice. A working example:

procedure TForm1.Test1;
var
  I: Integer;
  Value, A: String;
  jValue: TJSONValue;
  JSonValue1, JSonValue2: TJSonValue;
  JArray, JArr: TJSONArray;
begin
  Value  := '[["A1","A2","A3"],["B1","B2","B3"]]';
  JsonValue1 := TJSonObject.ParseJSONValue(Value);
  try
    JArray := JsonValue1 as TJSONArray;
    for JsonValue2 in JArray do
    begin
      JArr := JsonValue2 as TJSONArray;
      A := JArr.Items[0].Value;
      ShowMessage(A);
    end;
  finally
    JsonValue1.Free;
  end;
end;
Sign up to request clarification or add additional context in comments.

2 Comments

You need to Free() the TJSONValue that ParseJSONValue() returns. So use another TJSONValue variable for the loop
@Remy Lebeau Thanks for the suggestion, I corrected.

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.