消息关闭
    暂无新消息!
我前台通过ajax请求 在后台返回一个json数组的字符串 我想要在前台把得到的字符串解析成一个json数组,具体如下:
前台: 
$.ajax({
        dataType:'json',//数据传输格式:json
        url:"../user/onloadtree",//请求的路径
        error:function(){
            //请求失败处理函数
            alert('亲,请求失败!');
        },
        success:function(data){
         alert(data); //返回的是一个字符串 我想得到一个json数组s
        }
    });
后台:
@RequestMapping("/onloadtree")
public String LoadTree(HttpServletResponse response){
ArrayList<Menu> menu= lmsuserservice.LoadTree(); //得到的是一个menu集合
String treeNodesJson = JSONArray.fromObject(menu).toString();//将结果转换成JSONArray对象的形式的字符串 
 System.out.println(treeNodesJson);//在此处可以输出对应的字符串  标记为①
 try {
 response.setCharacterEncoding("utf-8");
response.getWriter().write(treeNodesJson);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

后台①处我的结果是:
[{"code":"admin","id":1,"name":"出入库管理","pId":0,"url":"","value":"aa"},{"code":"storage","id":2,"name":"仓储管理","pId":0,"url":"","value":"bb"},{"code":"manger","id":3,"name":"用户管理","pId":0,"url":"","value":"cc"},{"code":"客户","id":4,"name":"客户关系管理","pId":0,"url":"","value":"dd"},{"code":"huoguan","id":21,"name":"货位管理","pId":2,"url":"","value":"bb1"},{"code":"rolequan","id":31,"name":"角色权限","pId":3,"url":"","value":"cc1"},{"code":"fenpei","id":32,"name":"分配","pId":3,"url":"","value":"cc2"}]
而我想在前台展示的是
var zNodes=[
            {
                "code": "admin",
                "id": 1,
                "name": "出入库管理",
                "pId": 0,
                "url": "",
                "value": "aa"
            },
            {
                "code": "storage",
                "id": 2,
                "name": "仓储管理",
                "pId": 0,
                "url": "",
                "value": "bb"
            },
            {
                "code": "manger",
                "id": 3,
                "name": "用户管理",
                "pId": 0,
                "url": "",
                "value": "cc"
            },
            {
                "code": "客户",
                "id": 4,
                "name": "客户关系管理",
                "pId": 0,
                "url": "",
                "value": "dd"
            },
            {
                "code": "huoguan",
                "id": 21,
                "name": "货位管理",
                "pId": 2,
                "url": "",
                "value": "bb1"
            },
            {
                "code": "rolequan",
                "id": 31,
                "name": "角色权限",
                "pId": 3,
                "url": "",
                "value": "cc1"
            },
            {
                "code": "fenpei",
                "id": 32,
                "name": "分配",
                "pId": 3,
                "url": "",
                "value": "cc2"
            }
        ];
这样的一个json数组!!!!

求大神支招啊 



7个回答

︿ 1
前台解析json数据

var  zNodes = JSON.parse( data );

就OK了,获取的是一个数组
︿ 1
没有理解 你的意思 。 不过 你直接 alert 的话 出现的应该 是 [Object, Object ] . 
你可以使用  for  进行遍历 下看看  有没有你 需要的内容
︿ 0
谢谢楼主发问,谢谢层主回答,小弟学习了,顺便帮顶,顺便拿点经验和分,嘿嘿嘿嘿