JSON与JAVA数据的转换
JavaBean,List,Map转成json格式
JS解析json数据(如何将json字符串转化为数组)
需要四個包:
commons-beanutils-1.8.3.jar
commons-lang-2.3.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
普通JavaBean(以User为例)转成json格式
1.转成JSONArray类型
User user = new User();
user.setUsername("cxl");
user.setPassword("1234");
JSONArray json = JSONArray.fromObject(user);
System.out.println(json);//[{"password":"1234","username":"cxl"}]
response.getWriter().print(json.toString());
在js文件取数据
$.getJSON("http://localhost:8080/jQueryDemo/servlet/UserServlet",null,function(data) {
alert(data[0].username);
alert(data[0].password);
});
2.转成JSONObject类型
JSONObject jsonObj = JSONObject.fromObject(user);
System.out.println(jsonObj);//{"password":"1234","username":"cxl"}
response.getWriter().print(jsonObj);
在js文件取数据
alert(data.username);
alert(data.password);
List转成json格式
List
User user = new User();
user.setUsername("cxl");
user.setPassword("1234");
User u = new User();
u.setUsername("lhl");
u.setPassword("1234");
users.add(user);
users.add(u);
1.转成JSONArray类型
JSONArray json = JSONArray.fromObject(users);
System.out.println(json.toString());
//[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}]
response.getWriter().print(json.toString());
js中取数据:alert(alert(data[0].username));
2.转成JSONObject类型
不可直接使用JSONObject jsonObj = JSONObject.fromObject(users);
可通过下列方式
JSONObject jsonObj = new JSONObject();
jsonObj.put("users", users);
jsonObj.put("u", u);
System.out.println(jsonObj);
{"users":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}],
"u":{"password":"1234","username":"lhl"}}
response.getWriter().print(jsonObj);
js中取数据:alert(data.users[0].username);
alert(data.u[0].username);
Map转成json格式
Map
map.put("users", users);
map.put("u", u);
1.转成JSONArray类型
JSONArray json = JSONArray.fromObject(map);
System.out.println(json.toString());//
[{"users":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}],"u":{"password":"1234","username":"lhl"}}]
response.getWriter().print(json.toString);
js中取数据:alert(data[0].users[0].username);
2.转成JSONObject类型
JSONObject json = JSONObject.fromObject(map);
System.out.println(json);//
{"user":[{"password":"1234","username":"cxl"},{"password":"1234","username":"lhl"}],"u":{"password":"1234","username":"lhl"}}
response.getWriter().print(json);
js中取数据:alert(data.user[0].username);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var t="{'firstName': 'cyra', 'lastName': 'richardson', 'address': { 'streetAddress': '1 Microsoft way', 'city': 'Redmond', 'state': 'WA', 'postalCode': 98052 },'phoneNumbers': [ '425-777-7777','206-777-7777' ] }"; 10 var jsonobj=eval('('+t+')'); 11 alert(jsonobj.firstName); 12 alert(jsonobj.lastName); 13 14 15 var t2="[{name:'zhangsan',age:'24'},{name:'lisi',age:'30'},{name:'wangwu',age:'16'},{name:'tianqi',age:'7'}] "; 16 var myobj=eval(t2); 17 for(var i=0;i){ 18 alert(myobj[i].name); 19 alert(myobj[i].age); 20 } 21 22 var t3="[['14113295100','社旗县国税局桥头税务所','社旗县城郊乡长江路西段']]"; 23 //通过eval() 函数可以将JSON字符串转化为对象 24 var obj = eval(t3); 25 for(var i=0;i ){ 26 for(var j=0;j ){ 27 alert(obj[i][j]); 28 } 29 30 }