List集合转换成Json字符串
第一种方法:
public static JSONArray getJson(List<People> list) {
JSONArray json = new JSONArray();
try {
for (People people : list) {
JSONObject jo = new JSONObject();
jo.put("id", people.getName());
jo.put("time", people.getAge());
json.put(jo);
}
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
第二种方法:
导入com.alibaba:fastjson:1.2.56库
String json = JSON.toJSONString(list);
Map集合转换成Json字符串
导入com.google.code.gson:gson:2.8.5库
Gson gson=new Gson();
String json=gson.toJson(map);