日期:2014-05-17 浏览次数:20681 次
[{"name":"123","id":2,"password":"123","age":11,"birthday":653151600000,"sex":"男"},{"name":"123","id":3,"password":"123","age":123,"birthday":677170800000,"sex":"男"},{"name":"admin","id":7,"password":"admin","age":11,"birthday":653151600000,"sex":"男"}]
{"list":[{"id":3,"author":"dsadsa","title":"言情","price":123.0,"brief":"sdsada","bookName":"dsadsadsa"},{"id":4,"author":"dasd","title":"言情","price":123.0,"brief":"asda","bookName":"sadsa"}],"totalCount":2}
package com.dthh.util; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BeanUtils; public class Resultset { /** * 传入rs,sb * @param rs * @param sb */ public static void processResultsetString(ResultSet rs,StringBuffer sb) { try { ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount();//字段的数量 for (int i = 1; i <= columnCount; i++) { String columnName = rsmd.getColumnName(i);//获取到字段的名称 String value=rs.getString(columnName);//取值 sb.append(columnName+":'"+value+"'"); if(i!=columnCount){ sb.append(","); } } } catch (Exception e) { e.printStackTrace(); } } } 用法 StringBuffer sb = new StringBuffer(); sb.append("{data:["); while(rs.next()){ sb.append("{"); Resultset.processResultsetString(rs, sb); sb.append("}"); if(!rs.isLast()){ sb.append(","); } } sb.append("]}"); String json = sb.toString(); System.out.println(json);
------解决方案--------------------
帮顶。
------解决方案--------------------
用StringBuffer自己拼json
这是最常用的方法
祝楼主好运