Java, Php and other languages have mature frameworks for parsing Json data, which allows us to use very little code to convert formatted json data into objects or attributes that can be recognized by the program. At the same time, there are also such components in Delphi. Implement this functionality, i.e. IsuperObject. If you don’t have this component yet, please search and download it online or leave your email in the message box below to request it from me.
Let’s first talk about some commonly used functions in ISuperObject.
function SO(const s: SOString = '{}'): ISuperObject; overload; This function passes in the json data string and returns an ISuperObject object. This is usually the first function we use when parsing json, such as jObj: =SO(jsonstr).
property O[const path: SOString]: ISuperObject read GetO write PutO; default; For example: jobj.O['username'], this function is called by an ISuperObject object, the string in square brackets is the field name in json, returned An ISuperObject object.
property S[const path: SOString]: SOString read GetS write PutS; This function is called by an ISuperObject object. Different from O['username'], it returns a SoString, that is, a string. Use the method str: = jObj.S['username']; Similarly, there are several other similar functions, such as I['age'] returns an integer, B['isenable'] returns a Boolean type, and A['users'] returns a TSuperArray array.
AsString, AsBoolean, AsInteger, AsArray, ISuperObject functions are used to convert ISuperObject into the corresponding data type.
Let’s look at a demo code below. The json data is as follows
{ "retcode": "1", "datafrom": "server", "users": "[{/"id/":1, /"username/": /"liuderu/", /"website/": /"bcoder.com/"},{/"id/":2, /"username/": /"Jeoe/", /"website/": /"baidu.com/"}]"}Delphi version 2010, the code is as follows:
unit uFmMain;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons, superobject;type TFmMain = class(TForm) Memo1: TMemo; ListView1: TListView; BitBtn1: TBitBtn; Label1 : TLabel; procedure BitBtn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var FmMain: TFmMain;implementation{$R *.dfm}procedure TFmMain.BitBtn1Click(Sender: TObject);var jRet, jUsers: ISuperObject; aryUsers : TSuperArray; retCode: integer; strUsers: string; i: integer;begin jRet := SO(Memo1.Text); if (jRet.O['retcode'] <> nil) then begin retCode := jRet.O['retcode'].AsInteger; Label1 .Caption := 'Return value:' + IntToStr(retCode) + '; Data source:' + jRet.O['datafrom'].AsString; if(jRet.O['retcode'].AsInteger = 1) then begin strUsers := jRet.O['users'].AsString; jUsers := SO(strUsers); aryUsers := jUsers.AsArray; for I := 0 to aryUsers.Length - 1 do begin with ListView1.Items.Add do begin Caption := aryUsers[i].O['id'].AsString; SubItems.Add(aryUsers[i].O['username'].AsString); SubItems.Add(aryUsers[i ].O['website'].AsString); end; end; end; end;end;end.A simple example of Delphi using ISuperObject to parse json: Delphi_Json_jb51.rar