0
点赞
收藏
分享

微信扫一扫

如何把map转成json字符串

Yaphets_巍 2022-03-30 阅读 80
java后端

展开全部

Map转成32313133353236313431303231363533e58685e5aeb931333363393633json格式

Map map = new HashMap();

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);

举报

相关推荐

0 条评论