Shadow541
Shadow541

Reputation: 21

Parsing a Json array in Delphi

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?

Upvotes: 0

Views: 2063

Answers (1)

John I
John I

Reputation: 86

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;

Upvotes: 2

Related Questions