如何在Delphi 10 Seattle中解析此json数据? [英] How to parse this json data in Delphi 10 Seattle?

查看:140
本文介绍了如何在Delphi 10 Seattle中解析此json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi 10 Seattle中解析此JSON数据时遇到麻烦.我想从JSON中获取值,并一一显示在TLabel组件中.我是JSON和REST的新手,如果您提供一个有效的示例,那就太好了.

I am having trouble parsing this JSON data in Delphi 10 Seattle. I want to get the values from the JSON and show them in TLabel components one by one. I am new to JSON and REST, it would be nice if you provide a working example.

{
  "countrydata":[
    {
      "info":{
        "ourid":119,
        "title":"Pakistan",
        "code":"PK",
        "source":"https://thevirustracker.com/pakistan-coronavirus-information-pk"
      },
      "total_cases":5038,
      "total_recovered":1026,
      "total_unresolved":0,
      "total_deaths":86,
      "total_new_cases_today":27,
      "total_new_deaths_today":0,
      "total_active_cases":3926,
      "total_serious_cases":37,
      "total_danger_rank":33
    }
  ],
  "stat":"ok"
}

编辑 到目前为止,我已经有了这段代码,但是它给出了访问冲突错误,并且没有给出输出.我在做什么错了.

EDIT So far, I have this code but it gives access violation error and no output is given. What am I doing wrong.

procedure TForm1.Button2Click(Sender: TObject);
 var
  jsonRoot: TJSONObject;
  tokenRequest: TRESTRequest;
  tokenResponse: TRESTResponse;
  tokenClient: TRESTClient;
begin
  tokenClient := TRESTClient.Create(nil);
  tokenRequest := TRESTRequest.Create(nil);
  tokenResponse := TRESTResponse.Create(nil);
  try
    tokenRequest.Client := tokenClient;
    tokenRequest.Response := tokenResponse;
    tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';
    tokenRequest.Execute;
    jsonRoot:= TJSONObject.ParseJSONValue(tokenResponse.JSONText) as TJSONObject;
    Memo1.Lines.Add('TotalCases => ' + jsonRoot.GetValue('total_cases').Value);
    Memo1.Lines.Add('TotalRecovered=> ' + jsonRoot.GetValue('total_recovered').Value);
    Memo1.Lines.Add('TotalDeaths=> ' + jsonRoot.GetValue('total_deaths').Value);
    Memo1.Lines.Add('TotoalNewCases=> ' + jsonRoot.GetValue('total_new_cases_today').Value);
  finally
    tokenResponse.Free;
    tokenRequest.Free;
    tokenClient.Free;
  end;
end;

推荐答案

由于不正确使用TJSONObject,因此会出现访问冲突.

You are getting an Access Violation because you are using TJSONObject incorrectly.

您尝试读取的所有JSON值都不是您期望的JSON层次结构的顶级对象的直接子代,因此GetValue()返回一个nil指针,然后您的代码在崩溃时会崩溃尝试使用该nil指针读取Value属性.

All of the JSON values you are trying to read are not immediate children of the top level object of the JSON hierarchy, where you are expecting, so GetValue() returns a nil pointer, and then your code crashes when it tries to read the Value property using that nil pointer.

所需的值在JSON层次结构中更深一些级别.顶级对象包含名为countrydata的子级,该子级是对象的数组.您想要的值是该数组中第一个对象的子代.

The values you want are several levels deeper in the JSON hierarchy. The top level object contains a child named countrydata, which is an array of objects. The values you want are children of the 1st object in that array.

尝试以下方法:

procedure TForm1.Button2Click(Sender: TObject);
var
  jsonRoot: TJSONValue;
  jsonObj: TJSONObject;
  jsonArr: TJSONArray;
  tokenRequest: TRESTRequest;
  tokenResponse: TRESTResponse;
  tokenClient: TRESTClient;
begin
  tokenClient := TRESTClient.Create(nil);
  try
    tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';

    tokenRequest := TRESTRequest.Create(tokenClient);
    tokenRequest.Client := tokenClient;

    tokenResponse := TRESTResponse.Create(tokenClient);
    tokenRequest.Response := tokenResponse;

    tokenRequest.Execute;

    jsonRoot := TJSONObject.ParseJSONValue(tokenResponse.JSONText);
    try
      jsonObj := jsonRoot as TJSONObject;
      jsonArr := jsonObj.GetValue('countrydata') as TJSONArray;
      jsonObj := jsonArr.Items[0] as TJSONObject;
      Memo1.Lines.Add('TotalCases => ' + jsonObj.GetValue('total_cases').Value);
      Memo1.Lines.Add('TotalRecovered=> ' + jsonObj.GetValue('total_recovered').Value);
      Memo1.Lines.Add('TotalDeaths=> ' + jsonObj.GetValue('total_deaths').Value);
      Memo1.Lines.Add('TotoalNewCases=> ' + jsonObj.GetValue('total_new_cases_today').Value);
    finally
      jsonRoot.Free;
    end;
  finally
    tokenClient.Free;
  end;
end;

或者,您可以使用 TRESTResponse.RootElement TRESTResponse.JSONValue 属性,而不是调用TJSONObject.ParseJSONValue()手动:

Alternatively, you can use the TRESTResponse.RootElement and TRESTResponse.JSONValue properties instead of calling TJSONObject.ParseJSONValue() manually:

procedure TForm1.Button2Click(Sender: TObject);
var
  jsonObj: TJSONObject;
  jsonArr: TJSONArray;
  tokenRequest: TRESTRequest;
  tokenResponse: TRESTResponse;
  tokenClient: TRESTClient;
begin
  tokenClient := TRESTClient.Create(nil);
  try
    tokenClient.BaseURL := 'https://api.thevirustracker.com/free-api?countryTotal=PK';

    tokenRequest := TRESTRequest.Create(tokenClient);
    tokenRequest.Client := tokenClient;

    tokenResponse := TRESTResponse.Create(tokenClient);
    tokenRequest.Response := tokenResponse;
    tokenResponse.RootElement := 'countrydata';

    tokenRequest.Execute;

    jsonArr := tokenResponse.JSONValue as TJSONArray;
    jsonObj := jsonArr.Items[0] as TJSONObject;
    Memo1.Lines.Add('TotalCases => ' + jsonObj.GetValue('total_cases').Value);
    Memo1.Lines.Add('TotalRecovered=> ' + jsonObj.GetValue('total_recovered').Value);
    Memo1.Lines.Add('TotalDeaths=> ' + jsonObj.GetValue('total_deaths').Value);
    Memo1.Lines.Add('TotoalNewCases=> ' + jsonObj.GetValue('total_new_cases_today').Value);
  finally
    tokenClient.Free;
  end;
end;

这篇关于如何在Delphi 10 Seattle中解析此json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆