JSON Overview: JavaScript Object notation is a lightweight data interchange format.
JSON is essentially a string of data formats;
JavaScript can parse JSON directly, because JSON itself is an acoustic JavaScript data format. (JSON is an array or object inside JS)
JSON syntax rules:
The data is in the key value pair;
Data is separated by commas;
Curly braces Save the object;
Square brackets Save the array;
The JSON value can be:
Number (positive or floating point);
String (in double quotes);
Logical value (TRUE or false);
Array (in square brackets);
Object (in curly braces);
Null
eg: 1: {"Key1": value1, "Key2": value2,...} 2: [{"Key1": value1, "Key2": value2,...},{"Key1": value1, "Key2": value2,...},...]
a simple JSON data format example
{ "city": {"cid": 1, "CNAME": "Beijing"} }
Complex JSON Data example (there is a nesting between array objects)In the server-side JSON data generation tool:
1.fastjson Tools
2.json-lib Tools:
1 Jsonarray jsonarray = jsonarray.fromobject (list); // Converts an array or list collection to JSON; 2 // Jsonobject.fromobject (object): // turns the object or map collection into JSON; 3 Response.setcontenttype ("Text/xml;charset=utf-8"); 4 Response.getwriter (). Print (jsonarray.tostring ()); // to respond to Jsonarray data in string form
Json-lib Example 1
1 //The Jsonconfig object can be used when the returned object data needs to be filtered for extra information. 2Jsonconfig config =Newjsonconfig ();3Config.setexcludes (Newstring[]{"pid"});//sets the filtered member field as a string array,4Jsonarray Jsonarray = Jsonarray.fromobject (list,config);//overloads of the Fromobject method, passing jsonconfig into the5 //Jsonobject.fromobject (object)6Response.setcontenttype ("Text/html;charset=utf-8");7Response.getwriter (). Print (jsonarray.tostring ());
Json-lib Example 2Parsing JSON data on the client:
1 <HTML>2 <Head>3 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">4 <title>Hello Miss Dang</title>5 </Head>6 <Scriptsrc= "${pagecontext.request.contextpath}/js/jquery-1.11.3.min.js"></Script>7 <Scripttype= "Text/javascript">8 $(function(){9 $("#province"). Change (function(){Ten varPID= $( This). Val (); One $.post ("${pagecontext.request.contextpath}/cityjsonservlet",{"PID":p ID},function(data) { A $("#city"). HTML ("<option>-Please select-</option>"); - $ (data). each (function(i,n) { - $("#city"). Append ("<option value = '"+N.cid+"' >"+N.cname+"</option>"); the }); - },"JSON"); - }); - }); + </Script> - <Body> + <form> A <SelectID= "Province"> at <option>-Please select province-</option> - <C:foreachvar= "I"Items= "${list}"> - <optionvalue= "${i.pid}">${I.pname}</option> - </C:foreach> - </Select> - <SelectID= "City"> in </Select> - </form> to </Body> + </HTML>
View Code
jquery implementation of AJAX asynchronous request implementation of provincial and municipal linkage (data transfer format JSON)