java 字符串转json对象 js json 转 java对象

一、服务器端 字符串转json对象 需要json.jar包

              List<Map> list = new ArrayList<Map>();
		 for(int i = 0;i<3;i++){
			 Map map = new HashMap();
			 map.put("id", "id"+i);
			 map.put("name", "name"+i);
			 list.add(map);		 
		 }
		 Map jsonMap = new HashMap(); 
		 jsonMap.put("jsonstr",list);   //必须是map对象才能转换成json对象
		 JSONObject json = JSONObject.fromObject(jsonMap); 
		 out(json.toString());//将字符串传给客户端

二、客户端 json 转 java对象 需要json.js文件  可以通过ajax 异步接收 
var result = xmlHttp.responseText;
var jsonresult =JSON.parse(result);
var resultlist = jsonresult['jsonstr'];
for(var one in resultlist){
	var item = resultlist[one];
	alert(item.id);
	alert(item.name);
}


你可能感兴趣的:(java 字符串转json对象 js json 转 java对象)