Details of the JavaScript process for parsing JSON data

  • 2020-08-22 21:43:43
  • OfStack

JSON (JavaScript Object Notation) a simple data format that is lighter than xml. JSON is the JavaScript native format, which means that no special API or toolkits are required to process JSON data in JavaScript.

The rule of JSON is simple: an object is an unordered collection of 'name/value' pairs. 1 object begins with "{" (left parenthesis) and ends with"} "(right parenthesis). Each "name" is followed by a ":" (colon); Use ", "(comma) between" name/value "pairs. Details refer to http: / / www json. org/json - zh. html

Here's 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 represents an user object with attributes such as username, age, info, address, etc.

You can also use JSON to simply modify the data, as shown in the above 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);  
  user.username = "Tom";  
  alert(user.username);  
  } 

JSON provides json js package, download http: / / www json. org/json js, after introducing its then you can simply use object. toJSONString () converts 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 Object

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());  
  }

Write an ajax example of JSON using prototype.

Write a servlet (I was servlet ajax. JSONTest1. java) write 1 sentence

java code


response.getWriter().print("{ \"name\": \"Violet\", \"occupation\": \"character\" }");  

Write another request for ajax on 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);  
  }

prototype-1.5.1.js provides JSON method, ES104en. evalJSON(), you can modify the above method without using ES106en. js

js code


function jsonResponse(originalRequest) {  
  alert(originalRequest.responseText);  
  var myobj = originalRequest.responseText.evalJSON(true);  
  alert(myobj.name);  
  }  

JSON also provides java http jar package: / / www json. org/java/index html API is also very simple, for example below

Add the request parameter 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  
  }  
  );  
  }  

Using the JSON request string, you can simply generate JSONObject and parse it, modify servlet to add JSON processing (to use ES140en.jar)

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 JSON strings and modify servlet

java 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);  
  } 
0

The above content is to introduce JavaScrip processing t analysis JSON data process details, hope to help you.


Related articles: