JSON SuperObject (2) Construction Method and AsJSon in Delphi
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": "XXX", "No2": "YYY"}'; // Build from string procedure TForm1.Button1Click (Sender: TObject); var jo: ISuperObject; begin jo: = SO (JsonStr); {Or use the following statement, SO function is called TSuperObject.ParseString} // jo: = TSuperObject.ParseString (JsonStr); ShowMessage (jo.AsJSon (True, False)); end ; // Build from file procedure TForm1.Button2Click (Sender: TObject); const path ='c: \ temp \ json.txt'; var jo: ISuperObject; begin {Generate a test file; SuperObject does not support Chinese well, read the file it saves} SO (JsonStr) .SaveTo (path); {This generates and saves the json file} jo: = TSuperObject.ParseFile (path); ShowMessage (jo.AsJSon (True, False)); end ; // Build from stream procedure TForm1.Button3Click (Sender: TObject); var jo: ISuperObject; stm: TStream; b: Byte; begin {Simulate a test stream; see if the encoding it can accept is primitive enough, so does it for accessing files} stm: = TStringStream.Create ('{"No2": "\ u674e \ u56db", "No1": "\ u5f20 \ u4e09"}'); jo: = TSuperObject.ParseStream (stm); ShowMessage (jo.AsJSon (True, False)); stm.Free; end ; // AsJSon parameters 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 .