51,409
社区成员
发帖
与我相关
我的任务
分享 JSONArray array = JSONArray.fromObject(list);
this.jsonString = "{totalCount:" + list.size() + ",results:" + array.toString() + "}";
public static String List2JSONStr(List list, int totalRowCount) {
if (list == null || list.isEmpty()) {
return "{\"rowCount\":\"0\",\"rows\":[]}";
}
int len = list.size();
StringBuffer strBuf = new StringBuffer("{\"rowCount\":\"");
strBuf.append(totalRowCount != 0 ? totalRowCount : len).append("\",");
strBuf.append("\"rows\":").append("[ ");
Object obj = null;
JSONObject jsonObj = null;
for (int i = 0; i < len; i++) {
obj = list.get(i);
// if (!(obj instanceof Map)) continue;
if (obj instanceof Map)
jsonObj = JSONObject.fromObject(obj);
else
jsonObj = JSONObject.fromObject(Bean2Map(obj));
strBuf.append(jsonObj.toString());
strBuf.append(",");
}
strBuf.deleteCharAt(strBuf.length() - 1).append(" ]}");
return strBuf.toString();
}
看看是否适合你,在一个项目里见到过。