Delphi中使用ISuperObject解析Json數(shù)據(jù)的實(shí)現(xiàn)代碼
Java、Php等語言中都有成熟的框架來解析Json數(shù)據(jù),可以讓我們使用很少的代碼就把格式化好的json數(shù)據(jù)轉(zhuǎn)換成程序可識別的對象或者屬性,同時(shí)delphi中也有這樣的組件來實(shí)現(xiàn)此功能,即IsuperObject。如果還沒有這個(gè)組件的請?jiān)诰W(wǎng)上搜索下載或者在下面留言處留下你的郵箱向本人索取。
下面先說一下ISuperObject中幾個(gè)常用的函數(shù)
function SO(const s: SOString = ‘{}'): ISuperObject; overload; 此函數(shù)傳入json數(shù)據(jù)字符串,并返回一個(gè)ISuperObject對象,這一般是我們解析json時(shí)使用的第一個(gè)函數(shù),如jObj := SO(jsonstr)。
property O[const path: SOString]: ISuperObject read GetO write PutO; default; 如:jobj.O[‘username'],此函數(shù)被一個(gè)ISuperObject對象調(diào)用,方括號內(nèi)的字符串為json中的字段名稱,返回一個(gè)ISuperObject對象。
property S[const path: SOString]: SOString read GetS write PutS; 此函數(shù)被一個(gè)ISuperObject對象調(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ù),用來把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ù)來源:' + 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è)簡單的Delphi使用ISuperObject解析json的例子:Delphi_Json_jb51.rar
- 自定義spring mvc的json視圖實(shí)現(xiàn)思路解析
- C#實(shí)現(xiàn)json格式數(shù)據(jù)解析功能的方法詳解
- 實(shí)例解析Json反序列化之ObjectMapper(自定義實(shí)現(xiàn)反序列化方法)
- jquery利用json實(shí)現(xiàn)頁面之間傳值的實(shí)例解析
- JSONP跨域的原理解析及其實(shí)現(xiàn)介紹
- android客戶端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- JS解析json數(shù)據(jù)并將json字符串轉(zhuǎn)化為數(shù)組的實(shí)現(xiàn)方法
- 關(guān)于JSON解析的實(shí)現(xiàn)過程解析
相關(guān)文章
Delphi實(shí)例演示Rect、Bounds生成TRect的區(qū)別
這篇文章主要介紹了Delphi實(shí)例演示Rect、Bounds生成TRect的區(qū)別,需要的朋友可以參考下2014-07-07Delphi實(shí)現(xiàn)窗體感知鼠標(biāo)滑過并自動(dòng)隱藏與顯示窗口的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)窗體感知鼠標(biāo)滑過并自動(dòng)隱藏與顯示窗口的方法,涉及Delphi操作窗口及鼠標(biāo)事件的技巧,需要的朋友可以參考下2015-05-05delphi簡單判斷程序30秒沒有鍵盤和鼠標(biāo)動(dòng)作示例
本文為大家詳細(xì)介紹下delphi判斷程序30秒沒有鍵盤和鼠標(biāo)動(dòng)作,這里給timer設(shè)置了1000ms)的參數(shù),表示30秒的間隔,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Delphi實(shí)現(xiàn)判斷網(wǎng)址是否存在及是否可以打開的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)判斷網(wǎng)址是否存在及是否可以打開的方法,需要的朋友可以參考下2014-07-07