如何将Map或JavaBean的List对象转换为适合Ext之Grid的json串?

u010053185 2013-03-27 01:31:36
如何将Map或JavaBean的List对象转换为适合Ext之Grid的json串?
...全文
136 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
用 json-lib-2.2.1-jdk15.jar
 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();
	}


看看是否适合你,在一个项目里见到过。

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧