My current code:
procedure TForm1.Button10Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
JSON: string;
jsonObiekt: TJSONObject;
streams: TJSONValue;
liczbaStrumieni: integer;
i: integer;
begin
IdHTTP := TIdHTTP.Create;
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
IdHTTP.IOHandler := IdSSL;
IdHTTP.Request.CustomHeaders.AddValue('Accept', 'application/vnd.twitchtv.v3+json');
IdHTTP.Request.CustomHeaders.AddValue('Client-ID', 'smb61nyd0vxmqdn9d3k735qbx41cdyg');
JSON := IdHTTP.Get('https://api.twitch.tv/kraken/streams?game=StarCraft:%20Brood%20War');
finally
IdHTTP.Free;
end;
jsonObiekt := nil;
try
jsonObiekt := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(JSON), 0) as TJSONObject;
streams := jsonObiekt.Get('streams').JsonValue;
liczbaStrumieni := TJSONArray(streams).Size;
for i := 0 to liczbaStrumieni - 1 do
begin
Memo6.Lines.Add(TJSONObject(TJSONArray(streams).Get(i)).Get('viewers').JsonValue.Value);
end;
finally
jsonObiekt.Free;
end;
end;
With this code i am able to get viewers for every entries in "streams"
My goal is to get "status" from "channel", but thats too hard for me. Tried learn from this How to parse nested JSON object in Delphi XE2? but no result, still dont understand it well. Thanks for help.
Best Solution
It helps to break up the subobjects into their own variables, don't try to do everything in a single statement. Try this:
Alternatively, download the JSON as bytes so
TIdHTTP
will not decode them to UTF-16, and thusTJSONObject
can parse the original bytes as-is: