How to parse this json data in Delphi 10 Seattle?

筅森魡賤 提交于 2021-02-18 19:48:55

问题


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;

回答1:


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

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.

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.

Try this instead:

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;

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;


来源:https://stackoverflow.com/questions/61210350/how-to-parse-this-json-data-in-delphi-10-seattle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!