[code="java"]
jsonObject = new JSONObject();
jsonArray = JSONArray.fromObject(empList);
jsonObject.put("empList", jsonArray);
out.print(jsonObject);
按道理 我把集合轉換成json數據,然後輸出, 就可以在頁面裏面去訪問啊 但是我在 ext 中 用的時候不行,
就是直接 empList.*** 就可以了吧 請問誰知道不
[/code]
应该写成
jsonObject = new JSONObject();
jsonArray = JSONArray.fromObject(empList);
jsonObject.put("empList", jsonArray);
out.print(jsonObject.toString());
让它输出文本类型
前端用
var json = Ext.decode(responseText);
var empList = json.empList;
得到列表。
你打印出jsonObject,看看是什么
偶们是这么转的。。
[code="java"]List> mapList = new ArrayList>();
HashMap map;
[/code]
下面是个循环
[code="java"]{
map = new HashMap();
map.put("ID", 值);
map.put("字段1", 值);
map.put("字段2", 值);
mapList.add(map);
}[/code]
[code="java"] map = new HashMap();
map.put("totalProperty", 这是总数);
map.put("root", mapList);
JSONObject jsonObject = JSONObject.fromObject(map);
out.print(jsonObject);[/code]
你在Ext是怎么使用的?如果你拿到的是字符串文本,需要使用decode将JSON字符串转换为JS的JSON数据变量
[code="javascript"]
var json = Ext.util.JSON.decode(responseText);
var empList = json.empList;
[/code]
responseText就是out.print(jsonObject);返回的文本
你把你用ext的代码粘出来看,是哪里不行
前端js:
eval("var obj="+jsonStr);
然后就可以操作obj['empList']了