js之第三方工具解析JSON

1.JSON 隻是一種文本字符串。它被存儲在 responseText 屬性中

為瞭讀取存儲在 responseText 屬性中的 JSON 數據,需要根據 JavaScript 的 eval 函數。函數 eval 會把一個字符串當作它的參數。然後這個字符串會被當作 JavaScript 代碼來執行。因為 JSON 的字符串就是由 JavaScript 代碼構成的,所以它本身是可執行的

例如如下方式:

String json = "{username:'一葉扁舟',age:22}";
var  json = xhr.responseText;
json = eval("("+json+")");
alert(json.name);

2.利用第三方工具解析JSON

(1).需要導入的jar包


(2)代碼實戰:
domain實體類:vc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz48L3N0cm9uZz48cHJlIGNsYXNzPQ==”brush:java;”>package com.wenhao.web.ajax.domain;

/**
*類的作用:City實體類
*
*
*@author 一葉扁舟
*@version 1.0
*@創建時間: 2014-10-28 下午05:57:18
*/

//城市[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服務處理:

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;

/**
 *類的作用:JavaBean直接轉成JSON對象
 *這個是json傳輸
 *
 *
 *@author 一葉扁舟
 *@version 1.0
 *@創建時間: 2014-10-28   下午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("河南省")){
			cityList.add(new City(1,"鄭州"));
			cityList.add(new City(2,"洛陽"));
			cityList.add(new City(3,"開封"));
			cityList.add(new City(4,"信陽"));
		}else if(province.equals("福建省")){
			cityList.add(new City(1,"福州"));
			cityList.add(new City(2,"廈門"));
			cityList.add(new City(3,"莆田"));
			cityList.add(new City(4,"漳州"));
			cityList.add(new City(5,"龍巖"));
		}else if(province.equals("雲南省")){
			cityList.add(new City(1,"大理"));
			cityList.add(new City(2,"昆明"));
			cityList.add(new City(3,"香格裡拉"));
		}
		JsonConfig jsonConfig = new JsonConfig();
		//json數據中不包含id屬性
		jsonConfig.setExcludes(new String[]{"id"});
		//將cityList轉成json數組
		JSONArray jsonArray = JSONArray.fromObject(cityList,jsonConfig);
		jsonString = jsonArray.toString();
		System.out.println("jsonString="+jsonString);
		pw.write(jsonString);
		
	}

}

jsp界面獲取json數據:



  
	
  
  
	
		選擇省份	
		河南省	
		福建省	
		雲南省	
	
	
		選擇城市	
	
	<script type="text/javascript">
		document.getElementById("provinceID").onchange=function(){
			//清空原city列表框
			var citySelectElement = document.getElementById("cityID");
			var cityElementArray = citySelectElement.options;
			var size = cityElementArray.length;
			if(size > 1){
			//從後向前刪除下拉列表框中的東西
				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){
						//取得json字符串
						var jsonString = xhr.responseText;
						//將json字符串轉成json對象						
						var jsonObject = eval("("+jsonString+")");
						var size = jsonObject.length;
						for(var i=0;i<size;i++){
							var city = jsonObject[i].cityName;
							var optionElement = document.createElement("option");
							optionElement.innerHTML = city;
							document.getElementById("cityID").appendChild(optionElement);
						}
					}
				}
			}
			xhr.open("get","/FirstAjax/CityServlet?time="+new Date().getTime()+"&province="+province);
			xhr.send(null);
		};
		
		//創建一個ajax對象
		function createXHR(){
			var xhr = null;
			
			//如果是IE瀏覽器
			if(window.ActiveXObject){
				xhr = new ActiveXObject("microsoft.xmlhttp");
			}else{
		//非IE瀏覽器
				xhr = new XMLHttpRequest();
			}
			return xhr;
		}
	</script>
  




測試效果圖:

發佈留言