2

私はdelphi2012で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などを記述できるといいのですが、頭を包み込むにはサンプルを見る必要があります。

編集 これは2つの違いのスクリーンショットです。赤はドロップボックスサーバーからの応答では送信されませんが、アンマーシャラーによって予期されます。

違い

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ラッパーを使用してみてください。

于 2012-05-12T00:08:31.420 に答える