设为首页 加入收藏

TOP

Xe7 System.Json解析数据格式(三)
2017-10-10 12:06:39 】 浏览:5793
Tags:Xe7 System.Json 解析 数据 格式
---------------------------------------------------------- 开发工具:Delphi XE7 测试手机:华为荣耀X1
} unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, FMX.Layouts, FMX.Memo; type TForm1 = class(TForm) Panel1: TPanel; Memo1: TMemo; Panel2: TPanel; Button1: TButton; Button2: TButton; Memo2: TMemo; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormResize(Sender: TObject); private { Private declarations } // 重新设置button按钮 procedure ResetButton; public { Public declarations } end; var Form1: TForm1; const // 演示用的JSON jsonString = '{"name":"张三", "other":["中国","程序员"]}'; implementation {$R *.fmx} uses System.json; // Dephi自带的JSON单元 procedure TForm1.Button1Click(Sender: TObject); var JSONObject: TJSONObject; // JSON类 i: Integer; // 循环变量 temp: string; // 临时使用变量 jsonArray: TJSONArray; // JSON数组变量 begin if Trim(Memo1.Text) = '' then begin ShowMessage('要解析数据不能为空!'); end else begin JSONObject := nil; try { 从字符串生成JSON } JSONObject := TJSONObject.ParseJSONValue(Trim(Memo1.Text)) as TJSONObject; if JSONObject.Count > 0 then begin { 1,遍历JSON数据 } Memo2.Lines.Add('遍历JSON数据:' + #13#10); Memo2.Lines.Add('JSON数据数量:' + IntToStr(JSONObject.Count)); for i := 0 to JSONObject.Count - 1 do begin if i = 0 then begin temp := JSONObject.Get(i).ToString + #13#10;; end else begin temp := temp + JSONObject.Get(i).ToString + #13#10; end; end; { output the JSON to console as String } Memo2.Lines.Add(temp); Memo2.Lines.Add('------------------------------'); { 2,按元素解析JSON数据 } Memo2.Lines.Add('按元素解析JSON数据:' + #13#10); temp := 'name = ' + JSONObject.Values['name'].ToString + #13#10; Memo2.Lines.Add(temp); // json数组 jsonArray := TJSONArray(JSONObject.GetValue('other'));; if jsonArray.Count > 0 then begin // 得到JSON数组字符串 temp := 'other = ' + JSONObject.GetValue('other').ToString + #13#10; // 循环取得JSON数组中每个元素 for i := 0 to jsonArray.Size - 1 do begin temp := temp + IntToStr(i + 1) + ' : ' + jsonArray.Items[i] .Value + #13#10; end; end; Memo2.Lines.Add(temp); end else begin temp := '没有数据!'; Memo2.Lines.Add(temp); end; finally JSONObject.Free; end; end; end; // 清空显示数据 procedure TForm1.Button2Click(Sender: TObject); begin Memo1.Text := ''; Memo2.Text := ''; end; // 设置要解析的JSON数据 procedure TForm1.Button3Click(Sender: TObject); begin Memo1.Text := jsonString; end; // 设置要解析的JSON数据 procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Text := jsonString; end; procedure TForm1.FormResize(Sender: TObject); begin // 重新设置button按钮 self.ResetButton; end; // 重新设置button按钮 procedure TForm1.ResetButton; var buttonWidth: Integer; begin buttonWidth := self.Width div 3; Button1.Width := buttonWidth; Button2.Width := butt
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Asphyre引擎】发布了新版本V101 下一篇delphi中string的管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目