Nested json object deserializing using Delphi 2012

≡放荡痞女 提交于 2019-12-06 12:31:09

You could use my SvSerializer class for this task. First, you'll need to define your class which you want to serialize/deserialize, e.g.:

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;

Then add TDropbox object's instance to serializer:

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

And now you can serialize/deserialize this object through 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

May be you could try using Progdigy´s JSON Wrapper instead?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!