zoukankan      html  css  js  c++  java
  • Delphi XE7中使用JSON

    Delphi XE7有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了。我写的小例子只是对包含字符串和数组的JSON进行解析,这两种数据类型,我觉得是实际使用中最常用、有用的类型,所以我仅仅用这两种例子做演示!

    演示代码:

    [delphi] view plaincopy在CODE上查看代码片派生到我的代码片

    1. { 
    2.   功能:DelphiXE7中使用JSON 
    3.   ------------------------------------------------------------------------------ 
    4.   说明: 
    5.   1,使用Delphi自己带的JSON(system.json)。 
    6.   2,这仅仅是一个简单例子,以后还会增加演示功能。 
    7.   ------------------------------------------------------------------------------ 
    8.   注意: 
    9.   1,JSON类创建后,里面所有元素不用管释放,JSON类自己管理,千万不要画蛇添足啊!!!!!! 
    10.   ------------------------------------------------------------------------------ 
    11.   作者:孙玉良 QQ:14667479 Email:sunylat@163.com  修改时间:2014/11/23 00:13 
    12.   ------------------------------------------------------------------------------ 
    13.   开发工具:Delphi XE7 
    14.   测试手机:华为荣耀X1 
    15. }  
    16. unit Unit1;  
    17.   
    18.   
    19. interface  
    20.   
    21.   
    22. uses  
    23.   System.SysUtils, System.Types, System.UITypes, System.Classes,  
    24.   System.Variants,  
    25.   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,  
    26.   FMX.Layouts, FMX.Memo;  
    27.   
    28.   
    29. type  
    30.   TForm1 = class(TForm)  
    31.     Panel1: TPanel;  
    32.     Memo1: TMemo;  
    33.     Panel2: TPanel;  
    34.     Button1: TButton;  
    35.     Button2: TButton;  
    36.     Memo2: TMemo;  
    37.     Button3: TButton;  
    38.     procedure Button1Click(Sender: TObject);  
    39.     procedure Button2Click(Sender: TObject);  
    40.     procedure FormCreate(Sender: TObject);  
    41.     procedure Button3Click(Sender: TObject);  
    42.     procedure FormResize(Sender: TObject);  
    43.   private  
    44.     { Private declarations }  
    45.   
    46.   
    47.     // 重新设置button按钮  
    48.     procedure ResetButton;  
    49.   public  
    50.     { Public declarations }  
    51.   end;  
    52.   
    53.   
    54. var  
    55.   Form1: TForm1;  
    56.   
    57.   
    58. const  
    59.   // 演示用的JSON  
    60.   jsonString = '{"name":"张三", "other":["中国","程序员"]}';  
    61.   
    62.   
    63. implementation  
    64.   
    65.   
    66. {$R *.fmx}  
    67.   
    68.   
    69. uses  
    70.   System.json; // Dephi自带的JSON单元  
    71.   
    72.   
    73. procedure TForm1.Button1Click(Sender: TObject);  
    74. var  
    75.   JSONObject: TJSONObject; // JSON类  
    76.   i: Integer; // 循环变量  
    77.   temp: string// 临时使用变量  
    78.   jsonArray: TJSONArray; // JSON数组变量  
    79. begin  
    80.   
    81.   
    82.   if Trim(Memo1.Text) = '' then  
    83.   begin  
    84.     ShowMessage('要解析数据不能为空!');  
    85.   end  
    86.   else  
    87.   begin  
    88.     JSONObject := nil;  
    89.     try  
    90.       { 从字符串生成JSON }  
    91.       JSONObject := TJSONObject.ParseJSONValue(Trim(Memo1.Text)) as TJSONObject;  
    92.   
    93.   
    94.       if JSONObject.Count > 0 then  
    95.       begin  
    96.   
    97.   
    98.         { 1,遍历JSON数据 }  
    99.         Memo2.Lines.Add('遍历JSON数据:' + #13#10);  
    100.   
    101.   
    102.         Memo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count));  
    103.   
    104.   
    105.         for i := 0 to JSONObject.Count - 1 do  
    106.         begin  
    107.   
    108.   
    109.           if i = 0 then  
    110.           begin  
    111.             temp := JSONObject.Get(i).ToString + #13#10;;  
    112.           end  
    113.           else  
    114.           begin  
    115.             temp := temp + JSONObject.Get(i).ToString + #13#10;  
    116.           end;  
    117.   
    118.   
    119.         end;  
    120.   
    121.   
    122.         { output the JSON to console as String }  
    123.         Memo2.Lines.Add(temp);  
    124.   
    125.   
    126.         Memo2.Lines.Add('------------------------------');  
    127.   
    128.   
    129.         { 2,按元素解析JSON数据 }  
    130.         Memo2.Lines.Add('按元素解析JSON数据:' + #13#10);  
    131.         temp := 'name = ' + JSONObject.Values['name'].ToString + #13#10;  
    132.         Memo2.Lines.Add(temp);  
    133.   
    134.   
    135.         // json数组  
    136.         jsonArray := TJSONArray(JSONObject.GetValue('other'));;  
    137.         if jsonArray.Count > 0 then  
    138.         begin  
    139.   
    140.   
    141.           // 得到JSON数组字符串  
    142.           temp := 'other = ' + JSONObject.GetValue('other').ToString + #13#10;  
    143.   
    144.   
    145.           // 循环取得JSON数组中每个元素  
    146.           for i := 0 to jsonArray.Size - 1 do  
    147.           begin  
    148.             temp := temp + IntToStr(i + 1) + ' : ' + jsonArray.Items[i]  
    149.               .Value + #13#10;  
    150.           end;  
    151.   
    152.   
    153.         end;  
    154.   
    155.   
    156.         Memo2.Lines.Add(temp);  
    157.   
    158.   
    159.       end  
    160.       else  
    161.       begin  
    162.         temp := '没有数据!';  
    163.         Memo2.Lines.Add(temp);  
    164.       end;  
    165.   
    166.   
    167.     finally  
    168.       JSONObject.Free;  
    169.     end;  
    170.   end;  
    171.   
    172.   
    173. end;  
    174.   
    175.   
    176. // 清空显示数据  
    177. procedure TForm1.Button2Click(Sender: TObject);  
    178. begin  
    179.   Memo1.Text := '';  
    180.   Memo2.Text := '';  
    181. end;  
    182.   
    183.   
    184. // 设置要解析的JSON数据  
    185. procedure TForm1.Button3Click(Sender: TObject);  
    186. begin  
    187.   Memo1.Text := jsonString;  
    188. end;  
    189.   
    190.   
    191. // 设置要解析的JSON数据  
    192. procedure TForm1.FormCreate(Sender: TObject);  
    193. begin  
    194.   Memo1.Text := jsonString;  
    195. end;  
    196.   
    197.   
    198. procedure TForm1.FormResize(Sender: TObject);  
    199. begin  
    200.   // 重新设置button按钮  
    201.   self.ResetButton;  
    202. end;  
    203.   
    204.   
    205. // 重新设置button按钮  
    206. procedure TForm1.ResetButton;  
    207. var  
    208.   buttonWidth: Integer;  
    209. begin  
    210.   buttonWidth := self.Width div 3;  
    211.   
    212.   
    213.   Button1.Width := buttonWidth;  
    214.   Button2.Width := buttonWidth;  
    215.   Button3.Width := buttonWidth;  
    216. end;  
    217.   
    218.   
    219. end.  
  • 相关阅读:
    【算法笔记】多线程斐波那契数列
    RAID技术详解
    Mysql 语句汇总(性能篇)
    JS 网页打印解决方案
    MyEclipse修改
    几个需要学习的点和技术
    MyEclipse配色字体等配置的解决方案
    使用hibernate 分表做增删改查
    Web平台开发流程以及规范
    easyui使用总结
  • 原文地址:https://www.cnblogs.com/martian6125/p/9631028.html
Copyright © 2011-2022 走看看