Delphi JSON解析器

时间:2014-08-06 00:42:11

标签: delphi

我目前的代码:

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

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

1 个答案:

答案 0 :(得分:9)

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

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;