使用Delphi 2012对嵌套的json对象进行反序列化

时间:2012-05-11 21:49:04

标签: json delphi unmarshalling

我正在为delphi 2012中的dropbox工作。我遇到的问题是反序列化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,或类似的东西,但我需要看一个样本才能绕过它。

修改 这是两者之间差异的截图。红色不会从Dropbox服务器的响应中发送,但是由解组器预期。

Differences

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

您可以尝试使用Progdigy´s JSON Wrapper吗?