2013-07-04 65 views
0

從查詢中的SessionBean的ArrayList到JSON

public List getUserTransaction(User user, Balancestatus status) { 
return em.createNamedQuery("Balance.findLastFiveTransaction").setParameter("user", user).setParameter("status", status).getResultList(); 

我查詢

select b from Balance b join b.typecategory t where b.user = :user and b.typecategory = t and b.status = :status 

獲得在servlet的數據接收結果,並傳遞到JSON

PrintWriter out = response.getWriter(); 

user = userHandler.userCheck("[email protected]"); 
List<Balance> userTransaction = balance.getUserTransaction(user, outStatus); 
Gson gson = new Gson(); 
String json = gson.toJson(userTransaction); 

out.write(json); 

使用jQuery和輸出在嘗試解析控制檯

............. 
$.each(data, function(index, element) { 
console.log(element); 
}); 
.............. 

並且得到錯誤

TypeError: invalid 'in' operand obj [Break On This Error]

typeof length === "number" && length > 0 && (length - 1) in obj);

的json從ArrayList中到JSON

[{"id":"7","description":"evil data show","sum":666.00,"category":{"id":"1","name":"Wallet"},"typecategory":{"id":"4","name":"Home"},"user":{"id":"1","created":"Jun 5, 2013 1:44:45 PM","email":"[email protected]","pass":"mail","updated":"Jun 5, 2013 1:44:45 PM"},"status":{"id":"2","statusName":"out"}} ...........]

如何正確傳遞的ArrayList到JSON?使用JBoss AS 7.1

+0

找到的解決方案需要解析JSon並刪除空間。稍後添加完整的正確答案 – user2483213

回答

0

我的解決方案

$.parseJSON(data.trim());

if(data){ 
    var obj = $.parseJSON(data.trim()); 
    $.each(obj, function(index, element) { 
     $('#divRenderTable').append('<br>'+element.sum+" "+element.description+" "+element.typecategory.name); 
});