json - 如何在 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
}
}
解决方案
感谢您的许多回复。
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;结尾;
推荐阅读
- javascript - 如何在 Vue.js 中将 router.push 用于相同的路径、不同的查询参数
- c++ - 读取一组用户输入,升序排序,打印结果
- javascript - 与文件交互的最佳实践 (Javascript)
- asp.net - 在页面加载的生命周期中将数据存储在 HttpRequest 对象中
- c - Scanf() - %a 格式/转换说明符是什么?
- ruby-on-rails - 为什么哈希 a 对于 a[:b] 和 a["b"] 的返回结果不同?
- python - 如何使用 Airflow SSH 到具有 RSA 公钥/私钥的服务器?
- r - 使用 RSQLite 和 R 在本地数据库中写入大型固定宽度数据集
- css - tabindex="0" 的不可点击元素是否应该删除 :focus 蓝色轮廓?
- r - 在 R 中读取嵌套的 JSON