Delphi用QJSON解析JSON格式的数据_

上传人:鲁** 文档编号:473603429 上传时间:2023-10-13 格式:DOCX 页数:7 大小:13.08KB
返回 下载 相关 举报
Delphi用QJSON解析JSON格式的数据__第1页
第1页 / 共7页
Delphi用QJSON解析JSON格式的数据__第2页
第2页 / 共7页
Delphi用QJSON解析JSON格式的数据__第3页
第3页 / 共7页
Delphi用QJSON解析JSON格式的数据__第4页
第4页 / 共7页
Delphi用QJSON解析JSON格式的数据__第5页
第5页 / 共7页
点击查看更多>>
资源描述

《Delphi用QJSON解析JSON格式的数据_》由会员分享,可在线阅读,更多相关《Delphi用QJSON解析JSON格式的数据_(7页珍藏版)》请在金锄头文库上搜索。

1、Delphi用QJSON解析JSON格式的数据_ 原来用superobject来解析JSON已经够用了,可惜这个东东不能在移动段用法,于是找到QJSON来处理。 这是一个国内高手写开源免费的东西,赞一个。 加入数据如下: message:ok,status:1,state:3,data: time:2021-07-07 13:35:14,context:客户已签收, time:2021-07-07 09:10:10,context:离开 北京石景山营业厅 派送中,递送员 温,电话, time:2021-07-06 19:46:38,context:到达 北京石景山营业厅, time:2021-

2、07-06 15:22:32,context:离开 北京石景山营业厅 派送中,递送员 温,电话, time:2021-07-06 15:05:00,context:到达 北京石景山营业厅, time:2021-07-06 13:37:52,context:离开 北京_同城中转站 发往 北京石景山 营业厅, time:2021-07-06 12:54:41,context:到达 北京_同城中转站, time:2021-07-06 11:11:03,context:离开 北京运转中心驻站班组 发往 北京_ 同城中转站, time:2021-07-06 10:43:21,context:到达 北京运

3、转中心驻站班组, time:2021-07-05 21:18:53,context:离开 福建_厦门支公司 发往 北京运转中 心_航空, time:2021-07-05 20:07:27,context:已取件,到达 福建_厦门支公司 用QJSON解析如下: procedure TForm15.Button1Click(Sender: TObject); var aqjson,aqjsonarr : TQJSON; i : Integer; stime, scontext : string; begin aqjson := TQJSON.Create; aqjson.parse(memo1.l

4、ines.text); if aqjson.ValueByName(message, ) = ok then begin memo2.Clear; aqjsonarr := aqjson.ItemByName(data); for i := 0 to aqjsonarr.Count - 1 do begin stime := aqjsonarr.Itemsi.ValueByName(time, ); scontext := aqjsonarr.Itemsi.ValueByName(context, ); Memo2.Lines.Add(stime+-+scontext); end; end;

5、end; 可以看到QJSON的解析还是很便利的。 不过这种格式存在大量冗余数据每个数据项都携带了字段信息,其实可以只返回一次字段信息即可。 数据精简如下: message:ok,status:1,state:3,data: 2021-07-07 13:35:14,客户已签收, 2021-07-07 09:10:10,离开 北京石景山营业厅 派送中,递送员温,电话, 可以看到数组里面的串不再是JSON格式(Key:Value)的了,这时不能再用法ValueByName,而挺直用法Value。 procedure TForm15.Button2Click(Sender: TObject); var

6、 aqjson,aqjsonarr : TQJSON; i : Integer; stime, scontext : string; begin aqjson := TQJSON.Create; aqjson.parse(memo3.lines.text); if aqjson.ValueByName(message, ) = ok then begin memo2.Clear; aqjsonarr := aqjson.ItemByName(data); for i := 0 to aqjsonarr.Count - 1 do begin stime := aqjsonarr.Itemsi.V

7、alueByName(time, ); scontext := aqjsonarr.Itemsi.ToString; Memo2.Lines.Add(stime+-+scontext); end; end; end; 实际编码中,会存在返回图片到客户端的状况,假如也采纳JSON格式传输的话,需要把图片转成Base64格式的传包装,然后再传输到客户端解析。 这里是一个演示,首先把图片转成流: Image1.Picture.Graphic.SaveToStream(ss); 然后编码成base64格式的: EncodeStream(ss, ss1); 留意ss和ss1的定义: var ss: TM

8、emoryStream; ss1,ss2 : TStringStream; EncodeStream的调用需要引用EncdDecd.pas单元。 然后把流转成字符串 var sdata : string; begin . sData := ss1.DataString; . end; 再把该字符串包装到JSON串: var aqjson : TQJSON; begin aqjson := TQJSON.Create; aqjson.Parse(memo3.Lines.Text); . aqjson.AddArray(pic).Add.AsString :=sdata; . end; 这样图片就打包到JSON里面了,传到客户端以后,再反过来解析即可: ss2 := TStringStream.Create(aqjson.ItemByName(pic).Items0.value); DecodeStream(ss2,ss);/将base64字符流还原为内存流 ss.Position := 0; / 必需 Image2.Picture.Graphic.LoadFromStream(ss); 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号