Delphi 6 使用JSON
找谷哥大神 尋找Delphi json 幾乎都是D7 以上
且已經有釋出的 library可以直接套用
結果遇到剛好也是用D6 的Rilyu大大,參考了JSON 官網寫出
D6的檔案。
http://blog.csdn.net/rilyu/article/details/35804517
下載
https://github.com/rilyu/json4delphi
小弟不才跟著玩了一下code如下
procedure TForm1.Button1Click(Sender: TObject); var Json,json1: TJson; Str: String; multiArray : Array of Array of string; byteArray : Array of Byte; i,g:integer; begin Json := TJson.Create; Json1 := TJson.Create; try Json.Put('null-field', null); Json.Put('boolean-field-true', True); Json['boolean-field-false'].AsBoolean := not Json.Get('boolean-field-true').AsBoolean; Json['number-field'].AsNumber := 3.1415926535; Json['number-field-integer'].AsInteger := Json['number-field'].AsInteger; Json['string-field'].AsString := 'Hello world'; with Json.Put('array-field', empty).AsArray do begin // Put(empty); // Put( True); // Put('arrayobject-field-0'); with Put(empty).AsObject do begin Put('array-object-field-1', '1test'); Put('array-object-field-2', '2json4delphi6'); end; with Put(empty).AsObject do begin Put('array-object-field-1', '3ok'); Put('array-object-field-2', '4json4delphi6'); end; with Put(empty).AsObject do begin Put('array-object-field-1', '5ok'); Put('array-object-field-2', '6json4delphi6'); end; end; with Json.Put('object-field', empty).AsObject do begin Put('object-field-1', 90); Put('object-field-2', 6.6260755e-34); end; Str := Json.Stringify; edit1.Text:= '1:' +' '+Json['array-field'].AsArray.Items[0].Stringify; edit2.Text:= '2:' +' '+Json['array-field'].AsArray.Items[1].Stringify; edit3.Text:= '3:' +' '+Json['array-field'].AsArray.Items[2].Stringify;; edit4.Text:= ''; i:=0; for i:=0 to Json['array-field'].AsArray.Count -1 do begin Json1.JsonObject.Merge (Json['array-field'].AsArray.Items[i].AsObject); for g:=1 to 2 do begin edit4.Text:= edit4.Text +' '+ json1.Get('array-object-field-'+ inttostr( g)).AsString; end; json1.Clear; end; Memo1.Text:=Str; Json.Clear; Json.Parse(Str); //直接轉成JSON格式 Json.Parse('{"a":1,"b":1}'); edit5.Text:=Json.Stringify; finally Json.Free; end; end;
大家一起加入blogads 賺零用錢!!