How to count number of elements in a JSON array in Delphi How to count number of elements in a JSON array in Delphi json json

How to count number of elements in a JSON array in Delphi


The items field is an array, so retrieving it as a string is just wrong, so it makes sense that reading the array count via a string length would not work.

Try this instead:

uses  ..., System.JSON;var  JSONBook, JSONItem: TJSONObject;  JSONItems: TJSONArray;  CountItems: integer;begin  JSONBook := RESTResponse1.JSONValue as TJSONObject;  JSONItems := JSONBook.GetValue('items') as TJSONArray;  CountItems := JSONItems.Count;  ShowMessage(IntToStr(CountItems));  for i := 0 to CountItems-1 do  begin    JSONItem := JSONItems.Items[i] as TJSONObject;    ...  end;end;