gpt4 book ai didi

Delphi JSON 解析器

转载 作者:行者123 更新时间:2023-12-03 14:52:26 25 4
gpt4 key购买 nike

我当前的代码:

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;

通过此代码,我可以获取“流”中每个条目的查看者 enter image description here

我的目标是从“ channel ”获得“状态”,但这对我来说太难了。尝试从中学习How to parse nested JSON object in Delphi XE2?但没有结果,还是不太明白。感谢帮助。

最佳答案

这有助于将子对象分解为它们自己的变量,不要尝试在单个语句中完成所有操作。试试这个:

procedure TForm1.Button10Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
JSON: string;
jsonObiekt: TJSONObject;
streams: TJSONArray;
stream: TJSONObject;
channel: TJSONObject;
status: TJSONString;
liczbaStrumieni: integer;
i: integer;
begin
IdHTTP := TIdHTTP.Create;
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
IdHTTP.IOHandler := IdSSL;
IdHTTP.Request.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 := TJSONObject.ParseJSONValue(JSON) as TJSONObject;
try
streams := jsonObiekt.Get('streams').JsonValue as TJSONArray;
liczbaStrumieni := streams.Size;
for i := 0 to liczbaStrumieni - 1 do
begin
stream := streams.Get(i) as TJSONObject;
channel := stream.Get('channel').JsonValue as TJSONObject;
status := channel.Get('status').JsonValue as TJSONString;
Memo6.Lines.Add(status.Value);
end;
finally
jsonObiekt.Free;
end;
end;

或者,将 JSON 作为字节下载,这样 TIdHTTP 就不会将它们解码为 UTF-16,因此 TJSONObject 可以按原样解析原始字节:

procedure TForm1.Button10Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
IdSSL: TIdSSLIOHandlerSocketOpenSSL;
JSON: TBytesStream;
jsonObiekt: TJSONObject;
streams: TJSONArray;
stream: TJSONObject;
channel: TJSONObject;
status: TJSONString;
liczbaStrumieni: integer;
i: integer;
begin
jsonObiekt := nil;
try
JSON := TBytesStream.Create;
try
IdHTTP := TIdHTTP.Create;
try
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
IdHTTP.IOHandler := IdSSL;
IdHTTP.Request.Accept := 'application/vnd.twitchtv.v3+json';
IdHTTP.Request.CustomHeaders.AddValue('Client-ID', 'smb61nyd0vxmqdn9d3k735qbx41cdyg');
IdHTTP.Get('https://api.twitch.tv/kraken/streams?game=StarCraft:%20Brood%20War', JSON);
finally
IdHTTP.Free;
end;
jsonObiekt := TJSONObject.ParseJSONValue(JSON.Bytes, 0, JSON.Size) as TJSONObject;
finally
JSON.Free;
end;
streams := jsonObiekt.Get('streams').JsonValue as TJSONArray;
liczbaStrumieni := streams.Size;
for i := 0 to liczbaStrumieni - 1 do
begin
stream := streams.Get(i) as TJSONObject;
channel := stream.Get('channel').JsonValue as TJSONObject;
status := channel.Get('status').JsonValue as TJSONString;
Memo6.Lines.Add(status.Value);
end;
finally
jsonObiekt.Free;
end;
end;

关于Delphi JSON 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25150592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com