【问题标题】:ArrayList to JsonArrayList 到 Json
【发布时间】:2013-07-02 05:52:54
【问题描述】:

在 SessionBean 中接收查询结果

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("mail@mail.com");
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: 无效的“in”操作数 obj [中断这个错误]

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

我的 Json 从 ArrayList 到 Json

[{"id":"7","description":"邪恶数据 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":"mail@mail.com","pass":"mail","updated":"2013 年 6 月 5 日 下午 1:44:45"},"status":{"id":"2","statusName":"out"}} ......]

如何正确地将 ArrayList 传递给 Json?使用 JBoss AS 7.1

【问题讨论】:

  • 找到的解决方案需要解析 JSon 并删除空间。稍后添加完整的正确答案

标签: jquery json servlets jboss arraylist


【解决方案1】:

我的解决方案

$.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);
 });

【讨论】:

    【解决方案2】:

    如果这是一个 REST API,那么使用 RestEasy 怎么样?正是为此目的,并且可以自动执行此操作。

    http://docs.jboss.org/resteasy/docs/3.0.1.Final/userguide/html_single/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-09
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多