如何把map转成json字符串

时间:2025-03-31 07:14:45

展开全部

Map转成32313133353236313431303231363533e58685e5aeb931333363393633json格式

Map map = new HashMap();

(“users”, users);

(“u”, u);

1.转成JSONArray类型

JSONArray json = (map);

(());//

[{“users”:[{“password”:“1234”,“username”:“cxl”},

{“password”:“1234”,“username”:“lhl”}],“u”:{“password”:“1234”,“username”:“lhl”}}]

().print();

js中取数据:alert(data[0].users[0].username);

2.转成JSONObject类型

JSONObject json = (map);

(json);//

{“user”:[{“password”:“1234”,“username”:“cxl”},

{“password”:“1234”,“username”:“lhl”}],“u”:{“password”:“1234”,“username”:“lhl”}}

().print(json);

js中取数据:alert([0].username);