zoukankan      html  css  js  c++  java
  • qjson中把记录或类型或泛型数组转换为json字符串

    unit Unit4;
     
    interface
     
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
     
    type
      TForm4 = class(TForm)
        btn1: TButton;
        Memo1: TMemo;
        lbl1: TLabel;
        btn2: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
     
    /// <summary>
    /// 新手留意,一定要定义在implementation关键词的上方.
    /// </summary>
    type TRenLei = record
      name: string;
      age: Integer;
      sex: Boolean;
    end;
     
     
    var
      Form4: TForm4;
     
    implementation
     
    {$R *.dfm}
     
    uses qjson;
     
    procedure TForm4.btn1Click(Sender: TObject);
    var
      ARenLei: TRenLei;
      MyQj: TQJson;
    begin
      MyQj := TQJson.Create;
      try
        //这个例子比较好理解
        ARenLei.name := '奥巴马';
        ARenLei.age := 1;
        ARenLei.sex := True;
     
        MyQj.FromRecord<TRenLei>(ARenLei);
        Memo1.Lines.Clear;
        Memo1.Lines.Add(MyQj.AsJson);
      finally
        MyQj.Free;
      end;
    end;
     
    procedure TForm4.btn2Click(Sender: TObject);
    var
      RenLeiArray: TArray<TRenLei>;
      MyQj: TQJson;
    begin
      MyQj := TQJson.Create;
      try
        SetLength(RenLeiArray,2);
        with RenLeiArray[0] do
        begin
          name := '拜登';
          age := 1;
          sex := True;
        end;
        with RenLeiArray[1] do
        begin
          name := '希拉里';
          age := 2;
          sex := False;
        end;
     
        //这里不是很好理解了,我也不理解,这里用TArray<TRenLei>就可以把泛型数组转换为Json,谁知道原理的请下方留言。
        MyQj.FromRecord<TArray<TRenLei>>(RenLeiArray);
        Memo1.Lines.Clear;
        Memo1.Lines.Add(MyQj.AsJson);
      finally
        MyQj.Free;
      end;
    end;
     
    end.

    转自:http://blog.qdac.cc/?p=4189

  • 相关阅读:
    02
    循环语句的注意点
    unsigned/signed int/char类型表示的数值范围
    C程序设计语言(第二版)--- 习题选
    第一篇来自博客园的博客--哈哈希望大家点赞
    脆弱的GPS系统--摘抄《环球科学》
    【Python入门自学笔记专辑】——函数式编程
    c++标准库conio.h文件
    推荐几个配色和图标网站
    Ajax实现简单下拉选项
  • 原文地址:https://www.cnblogs.com/railgunman/p/11059037.html
Copyright © 2011-2022 走看看