public void testQueryNoXml() {
Session session = HibernateSessionFactory.getSession();
JSONArray array = new JSONArray();
try {
List list = session
.createSQLQuery(
"select cardid,cardreaderid,mapid,stayinterval from v_locatedata")
.setFirstResult(0)
.setMaxResults(10)
.list();
for (int i = 0; i < list.size(); i++) {
Object[] obj = (Object[]) list.get(i);
JSONObject row = new JSONObject();
row.put("cid", obj[0]);
row.put("crid", obj[1]);
row.put("mid", obj[2]);
row.put("stl", obj[3]);
array.add(row);
}
JSONObject json = new JSONObject();
json.put("rowcount", list.size());
json.put("rows", array);
System.out.println("sb.tostring: " + json);
} catch (RuntimeException re) {
log.error("==converse json failed==", re);
} finally {
session.close();
}
}
输出:
sb.tostring: {"rowcount":10,"rows":[{"cid":10352,"crid":17,"mid":1,"stl":"0天00:00:17"},{"cid":10583,"crid":3,"mid":1,"stl":"0天00:00:17"},{"cid":10783,"crid":17,"mid":1,"stl":"0天00:00:51"},{"cid":10913,"crid":3,"mid":1,"stl":"0天00:00:17"},{"cid":10352,"crid":17,"mid":1,"stl":"0天00:00:34"},{"cid":10583,"crid":17,"mid":1,"stl":"0天00:00:34"},{"cid":10913,"crid":17,"mid":1,"stl":"0天00:00:34"},{"cid":10352,"crid":17,"mid":1,"stl":"0天00:02:50"},{"cid":10583,"crid":17,"mid":1,"stl":"0天00:02:50"},{"cid":10783,"crid":17,"mid":1,"stl":"0天00:03:07"}]}
请问大家是如何处理的呢?
解决方案 »
- java.sql.SQLException: 从数据库取连接错误! DbConnectionPool is exhausted!
- 泛型接口的爱情
- 想请救大家一个java访问存储过程的问题
- 整合JBPM问题,找不到流程定义
- JasperReport与Ireport 参数传递问题,请大侠指点(在线等!!!)
- 初学者的struts问题..提示can not resolve symbol:class MessageResourses
- 高手请进,求助,急急急--如何用java操作在Excel文档中加入图片
- Weblogic Server 8.1 版 与Weblogic Platform 8.1 版有什么区别
- STRUTS1.0 升级到 1.1的问题.
- hibernate用DetachedCriteria同时查询没有外键关联的两个表
- hibernate问题
- 关于struts和hibernate的问题
pojo可添加一个有参的构造方法可选,直接将前台得来的jsonString用于实例化一个pojo实例。
也就是说,俺用了硬编码解决此问题。
没开环境, 说下思路吧.obj2string(obj):
c = obj的Class对象
fieldList = 获取c的属性列表
propertyList = new ArrayList();
遍历fieldList, 查看是否有对应的get和set方法, 如果有, 认为是property, 加入到propertyList中.
遍历propertyList, 获取每个property对应的json_string对此, 在性能上可以做一个优化, 在你的整个应用的生命周期内(web程序的applicationContext)建立一个已经支持json解析的缓存, 比如, supportJsonClass = new HashMap<Class, propertyList>(), 缓存的键是该类的Class对象. 对应的值是这种类需要解析的属性列表. 这样你就不用在每次解析之前, 先遍历获取需要输出的属性列表.
{
StringBuffer stringValue = new StringBuffer();
Class clazz = obj.getClass();
Map getMethodMap = getGetMethodMap(clazz);
Iterator iter = getMethodMap.keySet().iterator();
stringValue.append(" { ");
while(iter.hasNext())
{
String methodName = iter.next().toString();
Method md = (Method)getMethodMap.get(methodName);
Object value =md.invoke(obj,new Object[]{});
stringValue.append(dropCase(methodName));
stringValue.append(" : '");
stringValue.append(value);
stringValue.append("', ");
}
stringValue.append(" objclass : '");
stringValue.append(obj.getClass());
stringValue.append("'");
stringValue.append(" } ");
return stringValue.toString();
}
这个是我的方法。将pojo改成js可识别的json。如果楼主是list的 可以在函数外面加个循环或者在函数内加个循环。
呵呵, 最近都没有做java的东西了, 环境都没的......抱歉.
import java.util.*;class ListToJson{ public static void main(String[] args) throws Exception
{
// System.out.println(Class.forName("ttt").getDeclaredFields()[0].getModifiers());
List list=new ArrayList();
list.add(new ttt());
list.add(new ttt());
list.add(new ttt());
toJson(list);
} public static String toJson(List list) throws Exception
{
StringBuffer result=new StringBuffer();
if(list==null && list.size()==0)
{
return result.toString();
}
Object obj=list.get(0);
Method[] method=obj.getClass().getDeclaredMethods(); result.append("[");
for(int i=0;i<list.size();i++)
{
obj=list.get(i);
result.append("{");
for(int j=0;j<method.length;j++)
{
if(method[j].getName().startsWith("get"))
{
result.append(toFieldName(method[j].getName()));
result.append(":");
result.append(method[j].invoke(obj));
result.append(",");
}
}
result.deleteCharAt(result.length()-1);
result.append("}");
}
result.append("]");
System.out.println(result.toString());
return result.toString();
}
public static String toFieldName(String methodName)
{
String result="";
result+=(char)(methodName.charAt(3)+32);
for(int i=4;i<methodName.length();i++)
{
result+=methodName.charAt(i);
}
return result;
}
}
class ttt
{
public String a="tt";
private int b;
public String getA() {
return a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public void setA(String a) {
this.a = a;
}
}