如何把map转成json字符串
Map转成json格式
Map<String,Object> map = new HashMap<String,Object>();
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);