一.json常用转换方法
1.list
1List list = new ArrayList(); 2list.add("first"); 3list.add("second"); 4JSONArray jsonArray2 = JSONArray.fromObject(list);
2.map
1Map map = new HashMap(); 2map.put("name", "json"); 3map.put("bool", Boolean.TRUE); 4map.put("int", new Integer(1)); 5map.put("arr", new String[] { "a", "b" }); 6map.put("func", "function(i){ return this.arr[i]; }"); 7JSONObject json = JSONObject.fromObject(map);
3.bean
JSONObject jsonObject = JSONObject.fromObject(new JsonBean());
4.数组
1boolean[] boolArray = new boolean[] { true, false, true }; 2JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
5.一般数据
JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']" );
6.beans
1List list = new ArrayList(); 2JsonBean2 jb1 = new JsonBean2(); 3jb1.setCol(1); 4jb1.setRow(1); 5jb1.setValue("xx"); 6JsonBean2 jb2 = new JsonBean2(); 7jb2.setCol(2); 8jb2.setRow(2); 9jb2.setValue(""); 10list.add(jb1); 11list.add(jb2); 12JSONArray ja = JSONArray.fromObject(list);
二.HTML页面将String转换成json
eval()
1function testFunction() { 2 var jsonStr = '${str}'; 3 var dataObj = eval("(" + jsonStr + ")");//转换为json对象 4 $.each(dataObj.info, function(idx, item) { 5 alert("name:" + item.uuid + " , vaddress:" + item.vaddress); 6 }); 7}
三.后台向前台传json
1.使用转义符 \
2.交给前台来处理 eval()
3.使用json工具类
后台代码:
1 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 //1.返回标准的json字符串(双引号要用转义符) 3 //String s = "[{\"id\":1,\"text\":\"Root node\",\"children\":[{\"id\":2,\"text\":\"Child node 1\",\"children\":true},{\"id\":3,\"text\":\"Child node 2\"}]}]"; 4 5 //2.前台使用eval()函数将string装换成json格式 6 //String s = "[{id:1,text:'Root node',children:[{id:2,text:'Child node 1',children:true},{id:3,text:'Child node 2'}]}]"; 7 8 //3.使用json工具类 9 JSONArray s = getJson(); 10 response.setContentType("json"); 11 12 response.getWriter().print(s); 13 } 14 15 private JSONArray getJson() { 16 JSONObject jo1 = new JSONObject(); 17 JSONObject jo2 = new JSONObject(); 18 jo1.put("id", 2); 19 jo1.put("text", "Child node 1"); 20 jo2.put("id", 3); 21 jo2.put("text", "Child node 2"); 22 JSONArray jsonArray = new JSONArray(); 23 jsonArray.add(jo1); 24 jsonArray.add(jo2); 25 JSONObject jsonObject = new JSONObject(); 26 jsonObject.put("id", 1); 27 jsonObject.put("text", "Root node"); 28 jsonObject.put("children", jsonArray); 29 JSONArray json = new JSONArray(); 30 json.add(jsonObject); 31 return json; 32 }
前台代码:
1<div id="jstree"></div> 2 <script> 3 $(function() { 4 $.ajax({ 5 url : "IndexServlet", 6 datatype : "json", 7 success : function(o) { 8 create(o); 9 } 10 }); 11 }); 12 function create(data) { 13 //data = eval("(" + data + ")"); 14 $('#jstree').jstree({ 15 "core" : { 16 "data" : data 17 }, 18 }); 19 } 20 /* $(function() { 21 $('#jstree').jstree({ 22 'core' : { 23 'data' : { 24 "url" : "IndexServlet", 25 "data" : function(node) { 26 return { 27 "id" : node.id 28 }; 29 }, 30 "dataFilter" : function(o) { 31 o = eval("(" + o + ")"); 32 console.info(o); 33 return o; 34 } 35 } 36 } 37 }); 38 }); */ 39 </script>