<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
??? <title>form.html</title>
??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
??? <meta http-equiv="description" content="this is my page">
??? <meta http-equiv="content-type" content="text/html; charset=UTF-8">
? </head>
? <body>
? ???? ? <select>
?????? <option value="">请选择....</option>
???? </select>
???? <select>
???? ???? <option value="">请选择.....</option>
???? </select>
? </body>
? <script language="JavaScript">
? ????????? window.onload = function(){
?????????????? var xhr = createXmlHttpRequest();
??????????????
?????????????? xhr.onreadystatechange = function(){
??????????????????? if(xhr.readyState==4){
???????????????????????? if(xhr.status==200){
????????????????????????????? var data = xhr.responseText;
?????????????????????????????
????????????????????????????? alert(data);
?????????????????????????????
????????????????????????????? /*
????????????????????????????? * ajax模型中,使用eval()函数将接收到的文本内容转换成json数据格式
????????????????????????????? * ???? * 利用"()"将要转换的内容包装,eval()函数会强行将其转换成json数据格式
????????????????????????????? * ???? * 不用"()"将要转换的内容包装,eval()函数会将"{}"解释为空的语句块
????????????????????????????? */
????????????????????????????? var json = eval("("+data+")");
?????????????????????????????
????????????????????????????? for(var i=0;i<json.length;i++){
?????????????????????????????????? var provinceValue = json[i].province;
????????????????????????????? }
???????????????????????? }
??????????????????? }
?????????????? }
??????????????
?????????????? xhr.open("get","../jsonServlet",true);
??????????????
?????????????? xhr.send(null);
????????? }
????????? function createXmlHttpRequest(){
?????????????? var xmlHttp;
?????????????? try{??? //Firefox, Opera 8.0+, Safari
??????????????????? xmlHttp=new XMLHttpRequest();
?????????????? }catch (e){
??????????????????? try{??? //Internet Explorer
???????????????????????? xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
??????????????????? }catch (e){
???????????????????????? try{
????????????????????????????? xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
???????????????????????? }catch (e){}?
??????????????????? }
?????????????? }
?????????????? return xmlHttp;
????????? }? ?
? </script>
</html>
JsonServlet.java
public?class?JsonServlet?extends?HttpServlet {
???????public?void?doGet(HttpServletRequest request, HttpServletResponse response)
???????????????????throws?ServletException, IOException {
????????????response.setContentType(?"text/html;charset=utf-8");
????????????PrintWriter?out?= response.getWriter();? ? ? ? ? ?
//??????????String json = "[{'province':'吉林省'},{'province':'辽宁省'},{'province':'山东省'}]";??
?????????????//模拟查询数据库
????????????List<Province> list =?new?ArrayList<Province>();
????????????Province p1 =?new?Province(1,?"吉林省"?);
????????????Province p2 =?new?Province(1,?"辽宁省"?);
????????????Province p3 =?new?Province(1,?"山东省"?);
????????????list.add(p1);
????????????list.add(p2);
????????????list.add(p3);? ? ? ? ??
????????????JSONArray json = JSONArray.?fromObject(list);? ? ? ? ?
?????????????out.println(json.toString());? ?
??????}
???????public?void?doPost(HttpServletRequest request, HttpServletResponse response)
???????????????????throws?ServletException, IOException {
????????????response.setContentType(?"text/html");
????????????PrintWriter out = response.getWriter();
????????????doGet(request, response);
??????}
}
JsonUtil.java
public?class?JsonUtil {
???????*?将数组转换成String类型的JSON数据格式
???????public?static?String array2json(Object[] objects){? ? ?
????????????JSONArray jsonArray = JSONArray.?fromObject(objects);
?????????????return?jsonArray.toString();? ? ??
??????}
???????*?将list集合转换成String类型的JSON数据格式
???????public?static?String list2json(List list){? ? ?
????????????JSONArray jsonArray = JSONArray.?fromObject(list);
?????????????return?jsonArray.toString();
??????}
???????*?将map集合转换成String类型的JSON数据格式
???????public?static?String map2json(Map map){
????????????
????????????JSONObject jsonObject = JSONObject.?fromObject(map);
?????????????return?jsonObject.toString();?
??????}
???????*?将Object对象转换成String类型的JSON数据格式
???????public?static?String object2json(Object object){? ? ?
????????????JSONObject jsonObject = JSONObject.?fromObject(object);
?????????????return?jsonObject.toString();
??????}
???????*?将XML数据格式转换成String类型的JSON数据格式
???????public?static?String xml2json(String xml){? ? ? ? ? ?
????????????JSONArray jsonArray = (JSONArray)?new?XMLSerializer().read(xml);
?????????????return?jsonArray.toString();? ? ??
??????}
????????*?除去不想生成的字段(特别适合去掉级联的对象)
???????public?static?JsonConfig configJson(String[] excludes) {
????????????JsonConfig jsonConfig =?new?JsonConfig();
????????????jsonConfig.setExcludes(excludes);
????????????jsonConfig.setIgnoreDefaultExcludes(?true);
????????????jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.?LENIENT);
?????????????return?jsonConfig;
??????}
}
二、解析json数据格式
这里将使用两种json的解析库来对我们的json数据格式进行解析以及生成我们的json数据格式。
1.json-lib()