JSON (JavaScript Object Notation) is a simple data format that is lighter than XML. JSON is a native JavaScript format, which means that working with JSON data in JavaScript does not require any special APIs or toolkits.
The rules of JSON are simple: an object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
The rules are as follows:
1) The map is represented by a colon (":"). Name: Value
2) The data is separated by commas (","). Name 1: value 1, Name 2: Value 2
3) The set of Mappings (objects) are represented by curly braces ("{}"). {Name 1: value 1, Name 2: Value 2}
4) a set (array) of side data is expressed in square brackets ("[]").
[
{Name 1: value, Name 2: Value 2},
{Name 1: value, Name 2: Value 2}
]
5 The type that the element value can have: string, number, object, array, True, false, NULL
On the server side, the JSON string can be stitched up manually, or the JSON object can be stitched together and then parsed on the client.
1, on the server side
1. Manually Stitching strings
String json= "[{' Province ': ' Hubei '},{' province ': ' Henan '}]";
OUT.PRINTLN (JSON);
2, using the interface splicing (need to import Json_lib.jar package)
List<person> list=new arraylist<person> ();
Person P1=new person ("1", "Tom");
Person P2=new person ("2", "Mary");
Person P3=new person ("3", "Jack");
List.add (p1);
List.add (p2);
List.add (p3);
Jsonarray json=jsonarray.fromobject (list);//Convert to JSON object
Out.println (Json.tostring ());//Return is still a string type
2. Parsing JSON objects on the client
var Data=xhr.responsetext;
var json=eval ("(" +data+ ")");
alert (json.length);
for (var i=0;i< json.length;i++)
{
alert (json[i].id);
alert (json[i].name);
}
/*
* Use the Eval () function to convert the contents of the Eval () function to the JSON data format, or to the text content
* * Using the eval () function to convert the JSON data format, you need to use "()" to wrap the incoming parameters
* * If you do not use "()" to package, the incoming content is "{}", then the eval () function will recognize it as "empty statement block"
* * If you are using "()" to package, the incoming content is "{}", then the eval () function will forcibly convert it to JSON format
*
* * Plus "()" must be converted successfully
*/
1 // using JSON to define functions 2 var method = {3 add:function (A, b) {4 return a +b; 5 }6 } 7 8 method.add (3,4);
Javascript--json Object