Parsing JSON using a third-party js Tool
1. JSON is just a text string. It is stored in the responseText attribute.
To read JSON data stored in the responseText attribute, you need to follow the JavaScript eval function. The function eval treats a string as its parameter. Then the string is executed as JavaScript code. Because the JSON string is composed of JavaScript code, it is executable.
For example:
String json = "{username: 'youye Binzhou ', age: 22}"; var json = xhr. responseText; json = eval ("(" + json + ")"); alert (json. name );
2. parse JSON using a third-party tool
(1). jar package to be imported
(2) code practice:
Domain entity class:
Package com. wenhao. web. ajax. domain;/The Role Of The *** class: City entity class *** @ author * @ version 1.0 * @ Creation Time: 05:57:18 * // City [JavaBean] public class City {private int id; private String cityName; public City () {} public City (int id, String cityName) {super (); this. id = id; this. cityName = cityName;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getCityName () {return cityName;} public void setCityName (String cityName) {this. cityName = cityName ;}}
Servlet service processing:
Package com. wenhao. web. ajax. servlet; import java. io. IOException; import java. io. printWriter; import java. util. arrayList; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import net. sf. json. JSONArray; import net. sf. json. jsonConfig; import com. wenhao. web. ajax. domain. city;/*** class function: JavaBean is directly converted to a JSON object * This is a json transmission *** @ author @ * @ version 1.0 * @ creation time: 05:58:20 */public class CityServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String province = request. getParameter ("province"); byte [] buf = province. getBytes ("ISO8859-1"); province = new String (buf, "UTF-8"); response. setContentType ("text/html; charset = UTF-8"); PrintWriter pw = response. getWriter (); List
CityList = new ArrayList
(); String jsonString = null; if (province. equals ("Henan Province") {cityList. add (new City (1, "Zhengzhou"); cityList. add (new City (2, "Luoyang"); cityList. add (new City (3, "Kaifeng"); cityList. add (new City (4, "Xinyang");} else if (province. equals ("Fujian Province") {cityList. add (new City (1, "Fuzhou"); cityList. add (new City (2, "Xiamen"); cityList. add (new City (3, "Putian"); cityList. add (new City (4, "Zhangzhou"); cityList. add (new City (5, "Longyan");} else if (province. equals ("Yunnan Province") {cityList. add (new City (1, "Dali"); cityList. add (new City (2, "Kunming"); cityList. add (new City (3, "Shangri-La");} JsonConfig jsonConfig = new JsonConfig (); // The json data does not contain the id attribute jsonConfig. setExcludes (new String [] {"id"}); // convert cityList into a json array JSONArray jsonArray = JSONArray. fromObject (cityList, jsonConfig); jsonString = jsonArray. toString (); System. out. println ("jsonString =" + jsonString); pw. write (jsonString );}}
The jsp interface obtains json data:
<% @ Page language = "java" pageEncoding = "UTF-8" %>
Select ProvinceHenanFujianYunnan ProvinceSelect City<Script type = "text/javascript"> document. getElementById ("provinceID "). onchange = function () {// clear the original city list box var citySelectElement = document. getElementById ("cityID"); var cityElementArray = citySelectElement. options; var size = cityElementArray. length; if (size> 1) {// delete from the back to the front of the drop-down list box for (var I = size-1; I> 0; I --) {citySelectElement. removeChild (cityElementArray [I]);} var province = this [this. selectedIndex]. innerHTML; province = encodeURI (province); var xhr = createXHR (); xhr. onreadystatechange = function () {if (xhr. readyState = 4) {if (xhr. status = 200) {// obtain the json string var jsonString = xhr. responseText; // convert the json string to the json object var jsonObject = eval ("(" + jsonString + ")"); var size = jsonObject. length; for (var I = 0; I
Test: