JSON is a JavaScript Native format, that is, processing JSON data in JavaScript does not require any special APIs or toolkit. Next, this article describes how to process and parse JSON data using JavaScript. if you are interested, please refer to the simple JSON (JavaScript Object Notation) 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 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
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 );}
The above content is to introduce the detailed explanation of the process of processing t-parsed JSON data by using JavaScrip, and hope to help you.