这是我的代码:
public String getList() throws JSONException {
List<User> data = member.getCurrentMembers() ;
for (User u : data) {
JSONObject datas = new JSONObject();
datas.put("name", u.getUserName());
datas.put("account", u.getAccount());
datas.put("edit", u.getUserId());
datas.put("id", u.getUserId());
System.out.println(datas);
}
return str;
}
现在它将打印:
{"name":"aa","id":1,"edit":1,"account":"aa@qq.com"}
{"name":"qq","id":2,"edit":2,"account":"qq@qq.com"}
{"name":"ww","id":3,"edit":3,"account":"ww@qq.com"}
我想要的json格式是:
{
"Data": [
{"name":"aa","id":1,"edit":1,"account":"aa@qq.com"},
{"name":"qq","id":2,"edit":2,"account":"qq@qq.com"},
{"name":"ww","id":3,"edit":3,"account":"ww@qq.com"},
]
}
我能做什么? 请指导我。谢谢
答案 0 :(得分:0)
这可以解决问题
public String getList() throws JSONException {
List<User> data = member.getCurrentMembers() ;
JSONArray array = new JSONArray();
for (User u : data) {
JSONObject datas = new JSONObject();
datas.put("name", u.getUserName());
datas.put("account", u.getAccount());
datas.put("edit", u.getUserId());
datas.put("id", u.getUserId());
array.put(datas);
}
JSONObject result = new JSONObject();
result.put("Data", array);
}
希望它有所帮助 丹