i am making my first steps with Json and i am looking for a solution to get the number of items in an JasonArray.
I need the number to count up in a For Loop.
The following code is working for me, just the count up which is now set to 5 should be
replaced by number of items in the array. Already tried JsonArray.Count -1 which according to my research should be the right way, but just replacing the number by JsonArray.Count -1 results in
access violation.
var
JSonObject:TJSonObject;
JSonValue:TJSonValue;
JSOnArray:TJsonArray;
st:string;
id, name, description,sku,price: string;
i,j: integer;
begin
st := memo1.Text;
j := 1;
if Assigned(JSONArray) then
begin
For i := 0 to 5 -1 do
Begin
JSonObject := TJSonObject.Create;
JsonValue:=JSonObject.ParseJSONValue(st);
if (JSONValue is TJSONArray) then
Begin
id := ((JSONValue as TJSONArray).Items[i] as TJSonObject).Get('id').JSONValue.Value;
sku := ((JSONValue as TJSONArray).Items[i] as TJSonObject).Get('sku').JSONValue.Value;
description := ((JSONValue as TJSONArray).Items[i] as TJSonObject).Get('description').JSONValue.Value;
name := ((JSONValue as TJSONArray).Items[i] as TJSonObject).Get('name').JSONValue.Value;
price := ((JSONValue as TJSONArray).Items[i] as TJSonObject).Get('price').JSONValue.Value;
stringgrid1.Cells[1,j] := sku;
stringgrid1.Cells[2,j] := name;
stringgrid1.Cells[4,j] := description;
stringgrid1.Cells[3,j] := price;
j:=j+1;
End;
End;
JSonObject.Free;
end;