本文介绍了将记录数组存储在JSON中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过SuperObject库将记录数组存储在JSON中.例如.

How can an array of record be stored in JSON via SuperObject library. For example..

type
  TData = record
    str: string;
    int: Integer;
    bool: Boolean;
    flt: Double;
  end;

var
DataArray: Array[0..100] of TData;

推荐答案

只需使用超级对象编组 TSuperRTTIContext

Just use the superobject Marshalling TSuperRTTIContext

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  superobject,
  System.SysUtils;

type
  TData = record
    str : string;
    int : Integer;
    bool : Boolean;
    flt : Double;
  end;

  TDataArray = Array [0 .. 100] of TData;

procedure Test;
var
  DataArray : TDataArray;
  so :        ISuperObject;
  ctx :       TSuperRttiContext;
begin
  ctx := TSuperRttiContext.Create;
  try
    so := ctx.AsJson<TDataArray>( DataArray );
  finally
    ctx.Free;
  end;
  Writeln( so.AsJson );
end;

begin
  try
    Test;
  except
    on E : Exception do
      Writeln( E.ClassName, ': ', E.Message );
  end;

  ReadLn;

end.

这篇关于将记录数组存储在JSON中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 07:26