json解析list时,我原先的写法是:
1 Map<String, Object> map = new HashMap<String, Object>(); 2 map.put("totalCount", totalCount); 3 map.put("items", list); 4 response.setContentType("text/json;charset=utf-8"); 5 String jsonString = ""; 6 JSONArray _JSONObj = JSONArray.fromObject(map); 7 jsonString = _JSONObj.toString();
在浏览器中打印的是:
[{"totalcount":1,"items":[{"id":"100","limits":"‘1’","panel":"","text":"基本信息管理","leaf":"false","topnode":"000"}]}]
前台显示的是:
多了一层
之后处理json值,处理后的结果是:
{"totalcount":1,"items":[{"id":"100","limits":"‘1’","panel":"","text":"基本信息管理","leaf":"false","topnode":"000"}]}
原因:ext在解析json是默认将"[]"当成了一个对象的子属性,所以解析出错
在treepanel中的fields[]中是否加入leaf属性是有区别的,这可能是Ext的一个bug,如果加入了leaf,如下
fields:[‘id‘, ‘text‘, ‘leaf‘]
fields:[‘id‘, ‘text‘]
现象:如果加入‘leaf’,Ext将该rawdata默认有子node