Delphi中使用ISuperObject解析Json數(shù)據(jù)的實(shí)現(xiàn)代碼
Java、Php等語(yǔ)言中都有成熟的框架來(lái)解析Json數(shù)據(jù),可以讓我們使用很少的代碼就把格式化好的json數(shù)據(jù)轉(zhuǎn)換成程序可識(shí)別的對(duì)象或者屬性,同時(shí)delphi中也有這樣的組件來(lái)實(shí)現(xiàn)此功能,即IsuperObject。如果還沒(méi)有這個(gè)組件的請(qǐng)?jiān)诰W(wǎng)上搜索下載或者在下面留言處留下你的郵箱向本人索取。
下面先說(shuō)一下ISuperObject中幾個(gè)常用的函數(shù)
function SO(const s: SOString = ‘{}'): ISuperObject; overload; 此函數(shù)傳入json數(shù)據(jù)字符串,并返回一個(gè)ISuperObject對(duì)象,這一般是我們解析json時(shí)使用的第一個(gè)函數(shù),如jObj := SO(jsonstr)。
property O[const path: SOString]: ISuperObject read GetO write PutO; default; 如:jobj.O[‘username'],此函數(shù)被一個(gè)ISuperObject對(duì)象調(diào)用,方括號(hào)內(nèi)的字符串為json中的字段名稱,返回一個(gè)ISuperObject對(duì)象。
property S[const path: SOString]: SOString read GetS write PutS; 此函數(shù)被一個(gè)ISuperObject對(duì)象調(diào)用,和O[‘username']不同的是,它返回的是一個(gè)SoString,即一個(gè)字符串,使用方法 str := jObj.S[‘username']; 同理的還有其他幾個(gè)類似的函數(shù),如I[‘a(chǎn)ge']返回整數(shù),B[‘isenable']返回布爾型,A[‘users']返回一個(gè)TSuperArray數(shù)組
AsString, AsBoolean, AsInteger,AsArray,ISuperObject的函數(shù),用來(lái)把ISuperObject轉(zhuǎn)換成相應(yīng)的數(shù)據(jù)類型。
下面我們看一個(gè)演示代碼,json數(shù)據(jù)如下
{ "retcode": "1", "datafrom": "server", "users": "[{\"id\":1, \"username\": \"liuderu\", \"website\": \"bcoder.com\"},{\"id\":2, \"username\": \"Jeoe\", \"website\": \"baidu.com\"}]" }
Delphi版本2010,代碼如下:
unit uFmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons, superobject; type TFmMain = class(TForm) Memo1: TMemo; ListView1: TListView; BitBtn1: TBitBtn; Label1: TLabel; procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var FmMain: TFmMain; implementation {$R *.dfm} procedure TFmMain.BitBtn1Click(Sender: TObject); var jRet, jUsers: ISuperObject; aryUsers: TSuperArray; retCode: integer; strUsers: string; i: integer; begin jRet := SO(Memo1.Text); if (jRet.O['retcode'] <> nil) then begin retCode := jRet.O['retcode'].AsInteger; Label1.Caption := '返回值:' + IntToStr(retCode) + '; 數(shù)據(jù)來(lái)源:' + jRet.O['datafrom'].AsString; if(jRet.O['retcode'].AsInteger = 1) then begin strUsers := jRet.O['users'].AsString; jUsers := SO(strUsers); aryUsers := jUsers.AsArray; for I := 0 to aryUsers.Length - 1 do begin with ListView1.Items.Add do begin Caption := aryUsers[i].O['id'].AsString; SubItems.Add(aryUsers[i].O['username'].AsString); SubItems.Add(aryUsers[i].O['website'].AsString); end; end; end; end; end; end.
一個(gè)簡(jiǎn)單的Delphi使用ISuperObject解析json的例子:Delphi_Json_jb51.rar
上一篇:沒(méi)有了
欄 目:Delphi
下一篇:Delphi 調(diào)用外部程序并阻塞到外部程序中
本文標(biāo)題:Delphi中使用ISuperObject解析Json數(shù)據(jù)的實(shí)現(xiàn)代碼
本文地址:http://mengdiqiu.com.cn/a1/Delphi/8591.html
您可能感興趣的文章
- 01-10在Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像的圖文演示無(wú)錯(cuò)
- 01-10delphi建立、讀取、存貯INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲線圖
- 01-10delphi建立、讀取、存貯INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函數(shù)
- 01-10delphi中一個(gè)值得大家來(lái)考慮的DLL問(wèn)題
- 01-10初探Delphi中的插件編程


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10在Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像的圖
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10delphi 正弦曲線圖
- 01-10Delphi Command模式
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函
- 01-10delphi中一個(gè)值得大家來(lái)考慮的DLL問(wèn)題
- 01-10初探Delphi中的插件編程
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文