Tutorial-delphi's Open source JSON library: Superobject, Introduction to Usage

Source: Internet
Author: User

Puzzle Day Problem A statement is done ... Go back and dwell.

{$DEFINE UNICODE} in Superobject

That's it, this is the JSON-officially-recommended Delphi processing JSON package, address: http://www.progdigy.com/?page_id=6

Unicode encoding is automatically default in Superobject, and processing Chinese will appear garbled.

Note {$DEFINE UNICODE} is available.

var
J, A:isuperobject;//Can not be tsuperobject, although there are tsuperobject this class, but this is not so unreasonable.

begin
j:=tsuperobject.create (stobject);//Create a JSON object, if the parameter is Starray,j is a JSON array, parameters can also be other variable types, such as strings, values and so on, looks very powerful, But if the argument is a string that describes the JSON, you get an error. If you want to load from text, use J:=SO (JSON text);

j.i[' number ']:=123; is equal to {"number": 123}, similarly, I can also become s,b,c and the like, to represent the string, Boolean, currency type.
j.s[' {string} ']:= ' value ';//This is a bug: There is no effect with curly braces.

a:=tsuperobject.create (Starray);//Create a JSON array a
a.i[']:=111; Represents adding a 111 element to the a array. Notice that the value in [] is ', ' and the semantics of its [] in the array is changed in a subversive way.
a.i[2]:= "S";//3rd element value is "s", well, [] there's a lot of use in Superobject.
j[' arr ']:=a;//Add array A to J, whose key is named Arr

ShowMessage (J.tojson ()); View JSON text for J

J:=nil; Can't write J. Free

end; Let's look at what JSON is .

JSON(JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, Delphi,javascript, Perl, Python, etc.). These features make JSON an ideal data exchange language.   

in http://www.json.org/json-zh.html, JSON support for a variety of languages has been provided.

As you can see from its name, JSON was originally derived from JavaScript, and it was simple and clear to manipulate JSON data in JavaScript. You can use JSON data just like you do with class objects, such as

//example1:

jdata =http://blog.soso.com/qz.q/{"name": "Lao Wang", "Age": "+"};

str1 = jdata.name;//Lao Wang

str2 = jdata["Name"];//Lao Wang

//example2:

jdata =http://blog.soso.com/qz.q/{"name": "Lao Wang", "Age": "+", "address": {"Address1": "Fuzhou", "Address2": "Xiamen"};

str1 = jdata.address.address1;//Fuzhou

str2 = jdata["Address" ["Address1"];//Fuzhou

The version after Delphi2009 has been added to support JSON, no longer requires third-party class library support. But I like with Henri Gourvest development of Superobject, interested can be http://www.progdigy.com/?page_id=6 understand, the latest version is 1.24.

Superobject provides two class library file Superobject.pas,superxmlparser.pas, where Superobject.pas is used to manipulate JSON data, and Superxmlparser.pas is used to parse from XML data Out JSON data.

Simple Example

uses superobject;

procedure Tform1.button1click (sender:tobject);

var

Sotest:isuperobject;

begin

sotest: = So (' {' "name": "Zhang San", "Age": "+", "address": {"Address1": "Fuzhou", "Address2": "Xiamen"}} ');

//equivalent method Sotest: = Tsuperobject.parsestring (' {' name ': ' Zhang San ', ' age ': ' + ', ' address ': {"Address1": "Fuzhou", "Address2": "Xiamen "}} ');

showmessage (sotest[' name '). asstring);//Zhang San

ShowMessage (sotest[' address. Address1 ']. asstring);//Fuzhou

end;

The Superobject class library provides a convenient way to manipulate JSON-like data in JavaScript.


==========================================================================================
Here is an example of generating an array:
Procedure Tform3.button1click (Sender:tobject);
Var
Json,arrjson,arr:isuperobject;
I:integer;
Begin
Arrjson:=sa ([]);
Arr:=so ();
For i:=0 to 4 do
Begin
Json: = so;
json.s[' tittle ': = ' bt ' +inttostr (I);
json.i[' classification ']: = I;
json.i[' offercent ']: = I;
json.s[' hints ': = ' DJs ' +inttostr (i);
json.s[' content ': = ' nr ' +inttostr (i);
json.s[' date ': = ' twtime ' +inttostr (i);
json.s[' url ']: = ' wt_filename ' +inttostr (i);
ARRJSON.ASARRAY.ADD (Json);
End
arr.o[' A ']:=arrjson;
Memo1.text:= (arr.asstring);
End
End.
The interpretation of the words in turn is OK!
======================================================================= 
procedure Tform1.button5click (sender:tobject);
var
J, A:isuperobject;//Can not be tsuperobject, although there are tsuperobject this class, but this is not so unreasonable. begin
j:=tsuperobject.create (stobject);//Create a JSON pair
j.i[' number ']:=123; equals {"number": 123},.
j.s[' string ']:= ' value ';//This is a bug: There is no effect with curly braces. a:=tsuperobject.create (Starray);//Create a JSON array a
a.i[']:=111; Represents adding a 111 element to the a array. Notice that the value in [] is ', ' and the semantics of its [] in the array is changed in a subversive way.
a.i[' 2 ']:=12;//The 3rd element value is "s", well, [] there's a lot of use in Superobject.
j[' arr ']:=a;//Add array A to J, whose key is named ArrShowMessage (j.asstring ()); View JSON text for J
J:=nil; Can't write J. Free
end;

Tutorial-delphi's Open source JSON library: Superobject, Introduction to Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.