Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Delphi10中解析这个json数据?_Json_Delphi - Fatal编程技术网

如何在Delphi10中解析这个json数据?

如何在Delphi10中解析这个json数据?,json,delphi,Json,Delphi,我在Delphi10中解析这个JSON数据时遇到问题。我想从JSON中获取值,并在TLabel组件中逐一显示它们。我不熟悉JSON和REST,如果您能提供一个工作示例,那就太好了 { “国家数据”:[ { “信息”:{ “ourid”:119, “标题”:“巴基斯坦”, “代码”:“主键”, “来源”:https://thevirustracker.com/pakistan-coronavirus-information-pk" }, “案件总数”:5038, “回收总量”:1026, “未解决

我在Delphi10中解析这个JSON数据时遇到问题。我想从JSON中获取值,并在
TLabel
组件中逐一显示它们。我不熟悉JSON和REST,如果您能提供一个工作示例,那就太好了

{
“国家数据”:[
{
“信息”:{
“ourid”:119,
“标题”:“巴基斯坦”,
“代码”:“主键”,
“来源”:https://thevirustracker.com/pakistan-coronavirus-information-pk"
},
“案件总数”:5038,
“回收总量”:1026,
“未解决的总数”:0,
“死亡总数”:86,
“今日新增病例总数”:27,
“今日新死亡总数”:0,
“活跃案件总数”:3926,
“严重案件总数”:37,
“总危险等级”:33
}
],
“stat”:“ok”
}
编辑 到目前为止,我有这段代码,但它给出了访问冲突错误,并且没有给出任何输出。我做错了什么

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
,您将遇到访问冲突

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

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

请尝试以下方法:

procedure TForm1.按钮2点击(发送方:TObject);
变量
jsonRoot:TJSONValue;
jsonObj:TJSONObject;
jsonArr:TJSONArray;
令牌请求:TRESTRequest;
标记反应:树反应;
tokenClient:TRESTClient;
开始
tokenClient:=TRESTClient.Create(nil);
尝试
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);
尝试
jsonObj:=jsonRoot作为TJSONObject;
jsonArr:=jsonObj.GetValue('countrydata')作为TJSONArray;
jsonObj:=jsonArr.Items[0]作为TJSONObject;
Memo1.Lines.Add('TotalCases=>'+jsonObj.GetValue('total_cases').Value);
Memo1.Lines.Add('TotalRecovered=>'+jsonObj.GetValue('total_recovered').Value);
Memo1.Lines.Add('totalDeathers=>'+jsonObj.GetValue('total_Deathers').Value);
Memo1.Lines.Add('TotoalNewCases=>'+jsonObj.GetValue('total\u new\u cases\u today').Value);
最后
jsonRoot.Free;
终止
最后
免费;
终止
终止
或者,您可以使用和属性,而不是手动调用
TJSONObject.ParseJSONValue()

procedure TForm1.按钮2点击(发送方:TObject);
变量
jsonObj:TJSONObject;
jsonArr:TJSONArray;
令牌请求:TRESTRequest;
标记反应:树反应;
tokenClient:TRESTClient;
开始
tokenClient:=TRESTClient.Create(nil);
尝试
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:=作为TJSONArray的tokenResponse.JSONValue;
jsonObj:=jsonArr.Items[0]作为TJSONObject;
Memo1.Lines.Add('TotalCases=>'+jsonObj.GetValue('total_cases').Value);
Memo1.Lines.Add('TotalRecovered=>'+jsonObj.GetValue('total_recovered').Value);
Memo1.Lines.Add('totalDeathers=>'+jsonObj.GetValue('total_Deathers').Value);
Memo1.Lines.Add('TotoalNewCases=>'+jsonObj.GetValue('total\u new\u cases\u today').Value);
最后
免费;
终止
终止

对于StackOverflow来说,询问教程不是主题。到目前为止,您尝试了哪些不适合您的方法?你的代码看起来像什么?您在这方面遇到的实际问题是什么?请解释一下。我能够获得json格式的数据(如上所述)。我想从获取的json中读取值。对于标签上显示的总箱数(5038)的ex.值。感谢Remy Lebeau,但两个代码都给出以下错误。[dcc32 Error]CovidMain.pas(103):E2149类没有默认属性,[dcc32 Error]CovidMain.pas(103):E2015运算符不适用于此操作数类型。我是否需要添加一些uses类或缺少某些内容??我正在使用Delphi 10西雅图更新1,这与此代码有关吗???@AmjadTabassum尝试使用
jsonArr.Items[]
而不是
jsonArr[]
。我更新了我的例子。