码迷,mamicode.com
首页 > Web开发 > 详细

mormot生成和解析json

时间:2021-06-02 16:58:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:private   sharp   utils   messages   mfile   variant   imp   log   测试   

mormot生成和解析json

delphi7测试并通过。

unit Unit1;

interface

uses
  syncommons, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
//解析json
var
  js: TDocVariantData;
  json: string;
begin
  json := ‘{"tt":"1"}‘;
  js.InitJSON(json);
  Caption := js.GetValueByPath([‘tt‘]);
end;
(*
{
  "blockCount":3,
  "blocks":[
    {"FieldCount":1, "fields":[{"Name":"姓名", "Value":["张1", "张2","张三"]}]},
    {"FieldCount":1, "fields":[{"Name":"单位", "Value":["华2", "张2","张三"]}]},
    {"FieldCount":1, "fields":[{"Name":"单位", "Value":["华拓", "张2","张三"]}]} 
  ]
}
*)

procedure TForm1.Button2Click(Sender: TObject);
//解析json
var
  js, js2, js3: TDocVariantData;
begin
  js.InitJSONFromFile(‘tt.json‘);
  caption := DocVariantData(js.GetValueByPath([‘blocks‘])).value[0]; //{"FieldCount":1, "fields":[{"Name":"姓名", "Value":["张1", "张2","张三"]}]}
  js2.InitJSON(caption);
  caption := DocVariantData(js2.GetValueByPath([‘fields‘])).value[0]; //{"Name":"姓名", "Value":["张1", "张2","张三"]}
  js3.InitJSON(caption);
  Caption := js3.U[‘Name‘] + DocVariantData(js3.GetValueByPath([‘Value‘])).value[0]; //姓名张1
  caption := DocVariantData(DocVariantData(js.A[‘blocks‘].Value[1]).A[‘fields‘].Value[0]).A[‘Value‘].Value[0]; //华2
end;
(*
{"Name":"Str0","Age":0,"List":[1,"Hello",5,{"name":"咏南中间件","age":99}]}
{"Name":"Str1","Age":1,"List":[1,"Hello",5,{"name":"咏南中间件","age":99}]}
*)
procedure TForm1.Button3Click(Sender: TObject);
//生成json
var
  jo: Variant;
  i: Int64;
begin
  TDocVariant.New(jo);
  i := 0;
  while i < 2 do
  begin
    jo.Name := ‘Str‘ + IntToStr(i);
    jo.Age := i;
    jo.List := _JSon(‘[1,"Hello",5,{"name":"咏南中间件","age":99}]‘);
    Memo1.Lines.Add(VariantSaveJSON(jo));
    inc(i);
  end;
end;

end.

  

  

mormot生成和解析json

标签:private   sharp   utils   messages   mfile   variant   imp   log   测试   

原文地址:https://www.cnblogs.com/hnxxcxg/p/14827165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!