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
There is an entry-level Article , You can also refer to the complete JSON learning manual text
A simple example:
JSCode
Copy 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 a user object with attributes such as username, age, info, and address.
you can also use JSON to modify data. In the preceding example,
JS Code copy Code the code is as follows: function showjson () {
var user =
{< br> "username": "Andy",
"Age": 20,
"info ": {"tel": "123456", "cellphone": "98765" },< br> "Address":
[
{"city ": "Beijing", "postcode": "222333" },< br> {"city": "NewYork", "postcode ": "555666" }< br>]
}< br> 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 CodeCopy codeThe 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 CodeCopy codeThe 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 CodeCopy codeThe 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 CodeCopy codeThe 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 CodeCopy codeThe 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 CodeCopy codeThe 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
}
);
}
the JSON request string can be used to generate and parse a jsonobject, and modify the servlet to add JSON processing (JSON is required. jar)
JAVA code copy 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 ();
}< br> response. getwriter (). print ("{\" Name \ ": \" Violet \ ", \" occupation \ ": \" character \ "}");
}
you can also use jsonobject to generate a JSON string and modify servlet
JAVA code copy 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 ();
}< br> 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 ();
}< br> response. getwriter (). print (resultjson. tostring ();
}
JS Code copy Code the code is as follows: function jsonresponse (originalrequest) {
alert (originalrequest. responsetext);
var myobj = originalrequest. responsetext. evaljson (true);
alert (myobj. name);
alert (myobj. age);
}