2

我正在为 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"
            }
        },

我看过这个页面,认为它可能是我要找的东西,但它从来没有真正涉及如何使用 TTypeObjectReverter (我认为这是我需要使用的)而且我似乎找不到网上的例子。

实现这一目标的最佳方法是什么?我希望我可以只写一个 TTypeObjectReverter 或类似的东西,但我需要查看一个示例才能理解它。

编辑 这是两者之间差异的屏幕截图。红色不是在 Dropbox 服务器的响应中发送的,而是解组器所期望的。

差异

4

2 回答 2

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
于 2012-05-12T12:05:21.337 回答
2

也许您可以尝试使用Progdigy 的 JSON Wrapper代替?

于 2012-05-12T00:08:31.420 回答