首页 > 解决方案 > 如何在 Delphi 中解析这种类型的 JSON 数据?

问题描述

如何在 Delphi 中解析以下 Json?

这是我的第一篇文章,在问这个问题之前我已经尽可能多地搜索了,所以如果我以任何方式发错了,请告诉我。

我想在“records”数组中获取“name_of_centre”的值

感谢您的任何帮助。

procedure TForm1.Button1Click(Sender: TObject);
var
  i : integer;
  jsonRoot: TJSONValue;
  jsonObj: TJSONObject;
  jsonArr: TJSONArray;
begin
  jsonRoot := TJSONObject.ParseJSONValue(memo2.Lines.text);
  try
    jsonObj := jsonRoot as TJSONObject;
    jsonObj := jsonObj.GetValue('result') as TJSONObject;
    jsonArr := jsonObj.GetValue('records') as TJSONArray;
    showmessage( jsonArr.Count.ToString );  // works ok

    for i := 0 to jsonArr.Count - 1 do
    begin
      jsonObj := jsonArr.Items[i] as TJSONObject;
      showmessage( jsonObj.GetValue('name_of_centre').Value );   // error here
    end;
  finally
    jsonRoot.Free;
  end;
end;

我已经检查了 Delphi 解析具有多种数组类型的 Json?

如何在 Delphi 10 Seattle 中解析这个 json 数据? (尤其是这个)

和其他一些链接......但 JSON 格式似乎不同。

有什么建议吗?

{
    "help": "testing",
    "success": true,
    "result": {
        "resource_id": "data_resource",
        "fields": [
            {
                "type": "int4",
                "id": "_id"
            },
            {
                "type": "text",
                "id": "name_of_centre"
            },
            {
                "type": "text",
                "id": "location_of_centre"
            },
            {
                "type": "text",
                "id": "type_of_centre"
            },
            {
                "type": "text",
                "id": "owner"
            },
            {
                "type": "numeric",
                "id": "no_of_outlets"
            },
            {
                "type": "numeric",
                "id": "no_of_branches"
            }
        ],
        "records": [
            {
                "location_of_centre": "Kings Road",
                "no_of_outlets": "12",
                "no_of_branches": "0",
                "name_of_centre": "Kings Road Centre",
                "type_of_centre": "HC",
                "owner": "Private",
                "_id": 1
            },
            {
                "location_of_centre": "Queens",
                "no_of_outlets": "14",
                "no_of_branches": "1",
                "name_of_centre": "Queens Centre",
                "type_of_centre": "HC",
                "owner": "Public",
                "_id": 2
            }
        ],
        "_links": {
            "start": "ignore",
            "next": "ignore2"
        },
        "limit": 2,
        "total": 10
    }
}

标签: jsonparsingdelphi

解决方案


感谢您的许多回复。

Olivier:我在这个修改后的代码中包含了错误。

Peter:我尝试使用 jsonRoot.GetValue('result.records[0].name_of_centre'),它确实给了我 name_of_centre 的值。好的开始。但我希望得到这个代码来给我数组中的项目数,我迭代数组,而不是硬编码。谢谢。

雷米:奇怪的是,它今天有效。它不会在 showmessage 中得到 Invalid Typecast(jsonObj.GetValue('name_of_centre').Value);

fpiette:我使用 Delphi 10.3 RIO。

感谢大家的回复。

是否需要使用jsonRoot.Free;-- 我在 stackoverflow.com 上的帖子中看到了这个...... jsonObj.Free 怎么样?

jsonRoot := TJSONObject.ParseJSONValue(memo2.Lines.text);
try
  jsonObj := jsonRoot as TJSONObject;
  jsonObj := jsonObj.GetValue('result') as TJSONObject;
  showmessage( jsonObj.ToString );

  jsonArr := jsonObj.GetValue('records') as TJSONArray;

  showmessage( jsonArr.Count.ToString );

  for i := 0 to jsonArr.Count - 1 do
  begin
    jsonObj := jsonArr.Items[i] as TJSONObject;
    if jsonObj .GetValue('name_of_centre').Value = null then
      showmessage('null');

// 以前有一个无效的类型转换显示消息(jsonObj.GetValue('name_of_centre').Value); 结尾; 最后是 jsonRoot.Free;结尾;


推荐阅读