首页 技术 正文
技术 2022年11月8日
0 收藏 434 点赞 1,903 浏览 2138 个字
  1. <span style=”font-size:14px;”>一直想找如何解析JSON数据的说,今天终于找到有人发帖子了。之前有人说用superobject,Tlkjson,delphi json library,delphi  web unit等等。其实我是想找比较简单的解析方式。解析简单的json。下面是转载的坦然的源码。
  2. </span>
  1. unit Unit1;
  2. interface
  3. uses
  4. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  5. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,DBXJSON;
  6. type
  7. TForm1 = class(TForm)
  8. Button1: TButton;
  9. procedure Button1Click(Sender: TObject);
  10. private
  11. { Private declarations }
  12. public
  13. { Public declarations }
  14. end;
  15. var
  16. Form1: TForm1;
  17. implementation
  18. {$R *.dfm}
  19. const
  20. GJSONString =
  21. ‘{‘ +
  22. ‘    “name”: {‘+
  23. ‘        “A JSON Object”: {‘ +
  24. ‘          “id”: “1”‘ +
  25. ‘        },’ +
  26. ‘        “Another JSON Object”: {‘ +
  27. ‘          “id”: “2”‘ +
  28. ‘        }’ +
  29. ‘    },’ +
  30. ‘    “totalobjects”: “2”‘ +
  31. ‘}’;
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34. LJSONObject: TJSONObject;
  35. Value: TJSONValue;
  36. begin
  37. LJSONObject := nil;
  38. try
  39. LJSONObject := TJsonObject.Create;
  40. Value := TJSONValue.Create;
  41. { convert String to JSON }
  42. LJSONObject.Parse(BytesOf(GJSONString), 0);
  43. Value :=LJSONObject.GetValue(‘name’);
  44. ShowMessage(Value.ToString);
  45. finally
  46. LJSONObject.Free;
  47. end;
  48. end;
  49. end.

灰常好,在此谢谢博主。

但是这样处理中文的时候会出现乱码现象。我对代码稍微修改了一下:

  1. var
  2. jo:tjsonobject;
  3. jv:tjsonvalue;
  4. jsonstr:string;//要转换的json字符串
  5. begin
  6. jo:=nil;
  7. jsonstr:='{“name”:”流川枫”,”interest”:”与樱木吵架”};
  8. try
  9. jo:=tjsonobject.create;
  10. jo:=tjsonobject.parsejsonvalue(tencoding.utf8.getbytes(jsonstr),0) as tjsonobject;
  11. jv:=jo.get(‘interest’).jsonvalue;
  12. showmessage(jv.value);
  13. finally
  14. jo.Free;
  15. end;
  16. end;

终于能转换成中文了。
下面是关于jsonobject的解析(举一反三):

  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3. jsonstr: string;
  4. jvalue: tjsonvalue;
  5. jobj: tjsonobject;
  6. jpair: tjsonpair;
  7. jarray: tjsonarray;
  8. begin
  9. jsonstr:='{‘name’:’tom’,’password’:’tomcat’,’interests’:[‘mouse’,’meat’]}’;
  10. jvalue := tjsonobject.ParseJSONValue
  11. (tencoding.UTF8.GetBytes(jsonstr), 0);
  12. try
  13. jobj := jvalue as tjsonobject;
  14. jpair := jobj.Get(2); // get the third json pair
  15. jarray := jpair.JsonValue as tjsonarray; // pair value is an array [‘mouse’,’meat’]
  16. strresult := jarray.Get(0).value; // first element of array[‘mouse’,’meat’]
  17. showmessage(strresult);//it is mouse
  18. finally
  19. jvalue.Free;
  20. end;
  21. end;

 http://blog.csdn.net/syndicater/article/details/17371111

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297