Guidance:
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:
JS 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.
JS 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 a JSON. js package. After downloading http://www.json.org/json.js, you can simply convert it to JSON data using object.tojsonstring.
JS 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.
JS Code
Function myeval (){
VaR STR = '{"name": "Violet", "Occupation": "character "}';
VaR OBJ = eval ('+ STR + ')');
Alert (obj. tojsonstring ());
}
Or use the parsejson () method.
JS 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.Java code
Response. getwriter (). Print ("{/" name/":/" Violet/",/" occupation/":/" character /"}");
Then write an Ajax request in the page.
JS 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
JS 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
JS 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)
Java 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
Java 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 ());
}
JS Code
Function jsonresponse (originalrequest ){
Alert (originalrequest. responsetext );
VaR myobj = originalrequest. responsetext. evaljson (true );
Alert (myobj. Name );
Alert (myobj. Age );
}
Reference
Disclaimer: The copyright of the javaeye article belongs to the author and is protected by law. You shall not reprint this document without the written consent of the author. If the author agrees to reprint the document, the original source and author of the article must be indicated in a hyperlink.
Related Articles: Comparison of three modes of script installation for JavaScript integration 3-minute entry
Comment6 in total
After reading it, it's so nice to get to know more about JSON.
Sorry
Wangcheng, I had a good click, accidentally made me a newbie, sin
I have sent a text message and told the Administrator to change it.
Lighter writes
Sorry
Wangcheng, I had a good click, accidentally made me a newbie, sin
I have sent a text message and told the Administrator to change it.
Modified.
The latest JSON. js cannot be used in IE. The too much recursion error is reported in FF but can be used.
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:
JS 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.
JS 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 a JSON. js package. After downloading http://www.json.org/json.js, you can simply convert it to JSON data using object.tojsonstring.
JS 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.
JS Code
Function myeval (){
VaR STR = '{"name": "Violet", "Occupation": "character "}';
VaR OBJ = eval ('+ STR + ')');
Alert (obj. tojsonstring ());
}
Or use the parsejson () method.
JS 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.Java code
Response. getwriter (). Print ("{/" name/":/" Violet/",/" occupation/":/" character /"}");
Then write an Ajax request in the page.
JS 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
JS 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
JS 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)
Java 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
Java 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 ());
}
JS Code
Function jsonresponse (originalrequest ){
Alert (originalrequest. responsetext );
VaR myobj = originalrequest. responsetext. evaljson (true );
Alert (myobj. Name );
Alert (myobj. Age );
}
Reference
Too much recursion is likely to be a situation where JSON cannot be processed: Object circular reference, for example:
Code
VaR user = {ID: 1, name: 'Hi'}; user. Friends = [user]; user. tojsonstring (); // too much recursion error reported in Firefox
This is an inherent defect of JSON.
If you only need to support Firefox, you can use uneval (User) to obtain the JSON variant format. ie does not support
To be compatible with Firefox and IE, you can use libraries such as objot, phprpc, and buffalo for processing. Of course, the format is different from that of JSON.