zoukankan      html  css  js  c++  java
  • JSON 之 SuperObject(2): 构建方式与 AsJSon

    SuperObject 构建一个 JSON 的常用方法: 从字符串、从文件、从流.


    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    uses SuperObject;
    
    const JsonStr = '{"No1":"张三", "No2":"李四"}';
    
    //从字符串构建
    procedure TForm1.Button1Click(Sender: TObject);
    var
      jo: ISuperObject;
    begin
      jo := SO(JsonStr);
      {或者用使用下面语句, SO 函数就是调用了 TSuperObject.ParseString}
      //jo := TSuperObject.ParseString(JsonStr);
      ShowMessage(jo.AsJSon(True, False));
    end;
    
    //从文件构建
    procedure TForm1.Button2Click(Sender: TObject);
    const
      path = 'c:	empjson.txt';
    var
      jo: ISuperObject;
    begin
      {产生个测试文件; SuperObject 对中文支持也不太好, 读取它自己保存的文件吧}
      SO(JsonStr).SaveTo(path); {这就产生并保存了 json 文件}
    
      jo := TSuperObject.ParseFile(path);
      ShowMessage(jo.AsJSon(True, False));
    end;
    
    //从流构建
    procedure TForm1.Button3Click(Sender: TObject);
    var
      jo: ISuperObject;
      stm: TStream;
      b: Byte;
    begin
      {模拟个测试流; 看看它能接受的编码够原始的, 它存取文件也是如此}
      stm := TStringStream.Create('{"No2":"u674eu56db","No1":"u5f20u4e09"}');
    
      jo := TSuperObject.ParseStream(stm);
      ShowMessage(jo.AsJSon(True, False));
    
      stm.Free;
    end;
    
    //AsJSon 的参数
    procedure TForm1.Button4Click(Sender: TObject);
    var
      jo: ISuperObject;
    begin
      jo := SO(JsonStr);
    
      ShowMessage(jo.AsJSon);
      ShowMessage(jo.AsJSon(True));
      ShowMessage(jo.AsJSon(True, False));
      ShowMessage(jo.AsJSon(False, False));
    end;
    
    end.
  • 相关阅读:
    2018.11.7 PION 模拟赛
    洛谷 P1074 靶形数独
    洛谷 P2831 愤怒的小鸟
    2018.11.6 PION 模拟赛
    洛谷 P1034 矩形覆盖
    博客使用指南
    TERADATA SQL学习随笔<一>
    补发:用Meal Prep+模块化饮食来减肥之实操
    [从产品角度学excel 04]-单元格的“衣服”
    [从产品角度学EXCEL 03]-单元格的秘密
  • 原文地址:https://www.cnblogs.com/cpprun/p/4787953.html
Copyright © 2011-2022 走看看