Delphi xe10 对 json的解析应用


本文整理自网络,侵删。

 
Json语句
{"code":100,"state":"true","data":["hero","npc","pet"]}
引用单元

System.JSON  
类型说明

//申明变量  
Root:TJSONObject;  
//赋值  
Root:= TJSONObject.ParseJSONValue(Trim(JsonStr)) as TJSONObject;  

获取Json对象数量

Root.Count  

遍历对象名称及数值

for i:=0 to Root.count-1 do  
begin  
   memo1.lines.add(Root.Get(i).JsonString.toString + ' = ' + Root.Get(i).JsonValue.ToString);  
end;  
结果显示

"code" = 100  
"state" = "true"  
"data" = {"id":10,"name":"test"}  

获取指定对象内数值

Root.GetValue('data').ToString  
获取数组对象

申明json数组变量  
Arr:TJSONArray;  
Arr:=TJSONArray(Root.GetValue('data'));  
遍历json数组  
for i:=0 to Arr.Size - 1 do  
begin  
  memo1.lines.add(Arr.items[i].value);  
end;  

注意事项
Json解析无法判断数据内是否存在指定对象名,所以在吃不准是否存在指定对象的时候还是使用get遍历的方式读取数据比较稳妥。

相关阅读 >>

Delphi 在瑞星2010全保护下创建文件夹

Delphi 编写ie代理服务器

Delphi中exit,abort,break,continue 的区别

Delphi 解决idhttp下载数据时程序无响应的问题

winapi 字符及字符串函数(8): ischarupper - 是否是个大写字母

Delphi 合并两个 wav 文件流的函数

Delphi 增加/获得windows用户帐号

Delphi 隐藏进程代码

Delphi 用api函数抓取屏幕图像

Delphi 解决webbrowser不能复制的问题

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...