JSON (JavaScriptObjectNotation) 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
This is an entry-level article. You can also refer to the full manual text for JSON learning.
A simple example:
Js Code
The Code is as follows:
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
The Code is as follows:
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.
Js Code
The Code is as follows:
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
The Code is as follows:
Function myEval (){
Var str = '{"name": "Violet", "occupation": "character "}';
Var obj = eval ('+ str + ')');
Alert (obj. toJSONString ());
}
Or use the parseJSON () method.
Js Code
The Code is as follows:
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
The Code is as follows:
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
The Code is as follows:
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
The Code is as follows:
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
The Code is as follows:
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
The Code is as follows:
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
The Code is as follows:
Function jsonResponse (originalRequest ){
Alert (originalRequest. responseText );
Var myobj = originalRequest. responseText. evalJSON (true );
Alert (myobj. name );
Alert (myobj. age );
}