java中LIst转换成Json

时间:2022-05-19 21:48:57

List转换成json串

public String getNameListByID(Long Id)
{
List<Name> nameLists= nameService.selectNameById(Id);//获取List
if(null == nameLists){
return null;
}
JSONArray jsonArray = new JSONArray();
for(Name nameList : nameLists){
JSONObject jo = new JSONObject();
jo.put("NameCode", nameList.getNameCode());
jo.put("Name", nameList.getName());
jsonArray.add(jo);
}
return jsonArray.toString();
}