JSON (JavaScript Object Notation) is a simple data format, which is lighter than XML. JSON is a javascript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON rules are simple: the object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. Detailed reference http://www.json.org/json-zh.html
A simple example:
Program code
Function showjson (){
VaR user =
{
"Username": "Andy ",
"Age": 20,
"Info": {"tel": "123456", "cellphone": "98765 "},
"Address ":
[
{"City": "Beijing", "postcode": "222333 "},
{"City": "NewYork", "postcode": "555666 "}
]
}
Alert (user. username );
Alert (user. Age );
Alert (user.info. Cellphone );
Alert (user. Address [0]. City );
Alert (user. Address [0]. postcode );
}
This indicates that a user object has attributes such as username, age, info, and address.
You can also use JSON to modify the data.
Program code
Function showjson (){
VaR user =
{
"Username": "Andy ",
"Age": 20,
"Info": {"tel": "123456", "cellphone": "98765 "},
"Address ":
[
{"City": "Beijing", "postcode": "222333 "},
{"City": "NewYork", "postcode": "555666 "}
]
}
Alert (user. username );
Alert (user. Age );
Alert (user.info. Cellphone );
Alert (user. Address [0]. City );
Alert (user. Address [0]. postcode );
User. Username = "Tom ";
Alert (user. username );
}
JSON provides the JSON. js package, download the http://www.json.org/json.js, introduce it and then you can simply use object. tojsonstring () to convert to JSON data.
Program code
Function showcar (){
VaR Carr = new car ("Dodge", "Coronet R/T", 1968, "yellow ");
Alert (Carr. tojsonstring ());
}
Function car (make, model, year, color ){
This. Make = make;
This. Model = model;
This. Year = year;
This. Color = color;
}
You can use eval to convert JSON characters to objects.
Program code
Function myeval (){
VaR STR = '{"name": "Violet", "Occupation": "character "}';
VaR OBJ = eval ('+ STR + ')');
Alert (obj. tojsonstring ());
}
Or use the parsejson () method.
Program code
Function myeval (){
VaR STR = '{"name": "Violet", "Occupation": "character "}';
VaR OBJ = Str. parsejson ();
Alert (obj. tojsonstring ());
}
The following uses prototype to write a JSON Ajax example.
Write a servlet (My servlet. Ajax. jsontest1.java) to write a sentence.
Program code
Response. getwriter (). Print ("{/" name/":/" Violet/",/" occupation/":/" character /"}");
Then write an Ajax request in the page.
Program code
Function sendrequest (){
VaR url = "/mywebapp/jsontest1 ";
VaR mailajax = new Ajax. Request (
URL,
{
Method: 'get ',
Oncomplete: jsonresponse
}
);
}
Function jsonresponse (originalrequest ){
Alert (originalrequest. responsetext );
VaR myobj = originalrequest. responsetext. parsejson ();
Alert (myobj. Name );
}
The JSON method is provided in the prototype-1.5.1.js, String. evaljson (), you can modify the above method without using JSON. js
Program code
Function jsonresponse (originalrequest ){
Alert (originalrequest. responsetext );
VaR myobj = originalrequest. responsetext. evaljson (true );
Alert (myobj. Name );
}
JSON also provides Java jar package http://www.json.org/java/index.html API is also very simple, the following example
Add Request Parameters in Javascript
Program code
Function sendrequest (){
VaR Carr = new car ("Dodge", "Coronet R/T", 1968, "yellow ");
VaR pars = "Car =" + Carr. tojsonstring ();
VaR url = "/mywebapp/jsontest1 ";
VaR mailajax = new Ajax. Request (
URL,
{
Method: 'get ',
Parameters: pars,
Oncomplete: jsonresponse
}
);
}
JSON request string can be used to generate and parse jsonobject, modify servlet to add JSON processing (JSON. jar is required)
Program code
Private void doservice (httpservletrequest request, httpservletresponse response) throws ioexception {
String S3 = request. getparameter ("car ");
Try {
Jsonobject jsonobj = new jsonobject (S3 );
System. Out. println (jsonobj. getstring ("model "));
System. Out. println (jsonobj. getint ("year "));
} Catch (jsonexception e ){
E. printstacktrace ();
}
Response. getwriter (). Print ("{/" name/":/" Violet/",/" occupation/":/" character /"}");
}
You can also use jsonobject to generate a JSON string and modify the Servlet
Program code
Private void doservice (httpservletrequest request, httpservletresponse response) throws ioexception {
String S3 = request. getparameter ("car ");
Try {
Jsonobject jsonobj = new jsonobject (S3 );
System. Out. println (jsonobj. getstring ("model "));
System. Out. println (jsonobj. getint ("year "));
} Catch (jsonexception e ){
E. printstacktrace ();
}
Jsonobject resultjson = new jsonobject ();
Try {
Resultjson. append ("name", "Violet ")
. Append ("Occupation", "Developer ")
. Append ("Age", new INTEGER (22 ));
System. Out. println (resultjson. tostring ());
} Catch (jsonexception e ){
E. printstacktrace ();
}
Response. getwriter (). Print (resultjson. tostring ());
}
Program code
Function jsonresponse (originalrequest ){
Alert (originalrequest. responsetext );
VaR myobj = originalrequest. responsetext. evaljson (true );
Alert (myobj. Name );
Alert (myobj. Age );