2012-05-11 86 views
2

我正在處理delphi 2012中的dropbox包裝。我遇到的問題是反序列化json響應。當我做了我的賬戶文件夾和文件列表的請求,我得到的是看起來像這樣的迴應:使用Delphi 2012嵌套的json對象反序列化

{ 
    "hash": "some_hash", 
    "thumb_exists": false, 
    "bytes": 0, 
    "path": "/", 
    "is_dir": true, 
    "size": "0 bytes", 
    "root": "dropbox", 
    "contents": 
    [ 
     { 
      "revision": 11, 
      "rev": "b074cbcbb", 
      "thumb_exists": false, 
      "bytes": 0, 
      "modified": "Mon, 23 Apr 2012 19:19:27 +0000", 
      "path": "/Apps", 
      "is_dir": true, 
      "icon": "folder", 
      "root": "dropbox", 
      "size": "0 bytes" 
     }, 
     {  
      "revision": 142, 
      "rev": "8e074cbcbb", 
      "thumb_exists": false, 
      "bytes": 0, 
      "modified": "Wed, 09 May 2012 22:55:44 +0000", 
      "path": "/code", 
      "is_dir": true, 
      "icon": "folder", 
      "root": "dropbox", 
      "size": "0 bytes" 
     }, 
     { 
      "revision": 7, 
      "rev": "7074cbcbb", 
      "thumb_exists": false, 
      "bytes": 246000, 
      "modified": "Mon, 23 Apr 2012 18:40:51 +0000", 
      "client_mtime": "Mon, 23 Apr 2012 18:40:52 +0000", 
      "path": "/Getting Started.pdf", 
      "is_dir": false, 
      "icon": "page_white_acrobat", 
      "root": "dropbox", 
      "mime_type": "application/pdf", 
      "size": "240.2 KB" 
     } 
    ], 
    "icon": "folder" 
} 

我希望能夠解析使用TJSONUnMarshal對象,但事實證明,該TJSONUnMarshal預計JSON看起來像這個:

{ 
"type":"DropboxApiU.TFile", 
"id":1, 
"fields": 
{ 
    "hash": "some_hash", 
    "thumb_exists": false, 
    "bytes": 0, 
    "path": "/", 
    "is_dir": true, 
    "size": "0 bytes", 
    "root": "dropbox", 
    "contents": 
    [ 
     { 
      "type":"DropboxApiU.TFile", 
      "id":1, 
      "fields": 
      { 
       "revision": 11, 
       "rev": "b074cbcbb", 
       "thumb_exists": false, 
       "bytes": 0, 
       "modified": "Mon, 23 Apr 2012 19:19:27 +0000", 
       "path": "/Apps", 
       "is_dir": true, 
       "icon": "folder", 
       "root": "dropbox", 
       "size": "0 bytes" 
      } 
     }, 

我已經看了this頁面,並認爲這可能是什麼我要找的,但它從來沒有真正進入如何使用TTypeObjectReverter(其我認爲是我需要使用的),我似乎無法在網上找到一個例子。

發生這種情況的最佳方法是什麼?我希望我可以寫一個TTypeObjectReverter或類似的東西,但我需要看到一個樣本能夠圍繞它來包裝我的頭。

編輯 下面是兩者的區別截圖。紅色不會在收件箱服務器的響應中發送,但解組器可以預期。

Differences

+0

缺少第二個文件的結尾。無論如何,除了JSON文件將包括一些差異化工具的截圖也很好(這是週五,我懶得自己做:-) – TLama

+1

@TLama我上傳了一個屏幕截圖,第二個是缺少因爲所有的差異都顯示在第一部分,我懶得在這個光榮的星期五重建整個請求的方式德爾福期待它:) – nick

回答

4

您可以使用我的SvSerializer類來完成此任務。首先,您需要定義要序列化類/反序列化,如:

TDropbox = class 
    private 
    FHash: string; 
    Fthumb_exists: Boolean; 
    Fbytes: Integer; 
    Fpath: string; 
    Fis_dir: Boolean; 
    FSize: string; 
    Froot: string; 
    Fcontents: TArray<TContent>; 
    Ficon: string; 
    public 
    [SvSerialize] 
    property Hash: string read FHash write FHash; 
    [SvSerialize] 
    property thumb_exists: Boolean read Fthumb_exists write Fthumb_exists; 
    [SvSerialize] 
    property bytes: Integer read Fbytes write Fbytes; 
    [SvSerialize] 
    property path: string read Fpath write Fpath; 
    [SvSerialize] 
    property is_dir: Boolean read Fis_dir write Fis_dir; 
    [SvSerialize] 
    property size: string read FSize write FSize; 
    [SvSerialize] 
    property root: string read Froot write Froot; 
    [SvSerialize] 
    property contents: TArray<TContent> read Fcontents write Fcontents; 
    [SvSerialize] 
    property icon: string read Ficon write Ficon; 
    end; 

TContent = record 
    private 
    frevision: Integer; 
    Frev: string; 
    Fthumb_exists: Boolean; 
    Fbytes: Integer; 
    fmodified: string; 
    fclient_mtime: string; 
    fpath: string; 
    fis_dir: Boolean; 
    ficon: string; 
    froot: string; 
    fmime_type: string; 
    fsize: string; 
    public 
    property revision: Integer read frevision write frevision; 
    property rev: string read Frev write Frev; 
    property thumb_exists: Boolean read Fthumb_exists write Fthumb_exists; 
    property bytes: Integer read Fbytes write Fbytes; 
    property modified: string read fmodified write fmodified; 
    property client_mtime: string read fclient_mtime write fclient_mtime; 
    property path: string read fpath write fpath; 
    property is_dir: Boolean read fis_dir write fis_dir; 
    property icon: string read ficon write ficon; 
    property root: string read froot write froot; 
    property mime_type: string read fmime_type write fmime_type; 
    property size: string read fsize write fsize; 
    end; 

TDropbox對象的實例,然後添加到串行:

FSerializer := TSvSerializer.Create(); 
FDropboxFile := TDropbox.Create; 
FSerializer.AddObject('', FDropboxFile); 

現在你可以序列化/反序列化這個通過SvSerializer的對象:

FSerializer.DeSerialize(mmo1.Lines.Text{your json string, stream or filename}, TEncoding.UTF8{if it is string you must specify the encoding}); 
//After this line your FDropBoxFile's properties are filled from your json string 
+0

新來Delphi,你的類定義使用屬性構造幫助我解決了反序列化問題我在相當長的一段時間內都在爭鬥! – Santosh