Javascript:解析json响应时遇到问题

Javascript:解析json响应时遇到问题,javascript,jquery,json,spring,Javascript,Jquery,Json,Spring,Spring返回一个带有四个属性的json编码对象。其中之一是名为“array”的属性。我想要这个数组的内容 下面是整个json响应: ee {"map":null,"array":[{"id":2,"description":"Cloud For Dev","businessSize":2,"businessType":9,"businessLocation":3},{"id":3,"description":"Cloud For Prod","businessSize":2,"busines

Spring返回一个带有四个属性的json编码对象。其中之一是名为“array”的属性。我想要这个数组的内容

下面是整个json响应:

ee
{"map":null,"array":[{"id":2,"description":"Cloud For Dev","businessSize":2,"businessType":9,"businessLocation":3},{"id":3,"description":"Cloud For Prod","businessSize":2,"businessType":9,"businessLocation":3}],"string":null,"bool":false}
0
我不确定“ee”或0是什么意思。。。不管怎样,我试着这样解析它:

$.ajax({
    type: "GET",
    url: "/ajax/rest/teamService/list",
    dataType: "json",
    success: function (response) {
        var teamArray = response.array;

        var $el = $("#teamSelect");
        $el.empty(); 

        $.each(teamArray[0], function(team) {
                alert(team.description);
                $el.append($("<option></option>").attr("value", team.id).text(team.description));
        });

        // Reattach the plugin
        $("#teamSelect").selectbox("detach");
        $("#teamSelect").selectbox("attach");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        if (textStatus === 'error') {
            setTimeout(function() { window.location = '/do/login'; }, 7000);
        }
    }
});
$.ajax({
键入:“获取”,
url:“/ajax/rest/teamService/list”,
数据类型:“json”,
成功:功能(响应){
var teamArray=response.array;
变量$el=$(“#团队选择”);
$el.empty();
$.each(团队阵列[0],函数(团队){
警报(团队描述);
$el.append($(“”).attr(“value”,team.id).text(team.description));
});
//重新连接插件
$(“#团队选择”)。选择框(“分离”);
$(“#团队选择”)。选择框(“附加”);
},
错误:函数(jqXHR、textStatus、errorshown){
如果(textStatus=='error'){
setTimeout(函数(){window.location='/do/login';},7000);
}
}
});
我会弹出6次警报框(应该是2次),每次都会显示“未定义”,而不是实际描述

选择框本身有四个空选项

似乎我在迭代json编码对象的四个参数,而不是封闭数组的两个内容


如何修复此问题?

尝试此-
teamArray[0]
应仅为
teamArray

$.each(teamArray, function(i,team) {
    alert(team.description);
    $el.append($("<option></option>").attr("value", team.id).text(team.description));
});
$。每个(团队阵列,功能(i,团队){
警报(团队描述);
$el.append($(“”).attr(“value”,team.id).text(team.description));
});

尝试此操作-
teamArray[0]
应仅为
teamArray

$.each(teamArray, function(i,team) {
    alert(team.description);
    $el.append($("<option></option>").attr("value", team.id).text(team.description));
});
$。每个(团队阵列,功能(i,团队){
警报(团队描述);
$el.append($(“”).attr(“value”,team.id).text(team.description));
});

现在,您正在对
teamArray[0]
的键进行循环,因此出现了六个警报。循环
teamArray
。另外,
$。每个
的回调。也可以不通过jQuery:

for(var i = 0; i < teamArray.length; i++) {
    var team = teamArray[i];
    ...
}
for(var i=0;i
现在,您正在对
teamArray[0]
的键进行循环,因此出现了六个警报。循环
teamArray
。另外,
$。每个
的回调。也可以不通过jQuery:

for(var i = 0; i < teamArray.length; i++) {
    var team = teamArray[i];
    ...
}
for(var i=0;i
这根本不是有效的JSON字符串。那些“ee”和0破坏了语法,无法解析响应。我也对这两个位表示怀疑。我在fiddler显示的json响应中看到了它们,我不确定它们是什么意思。这根本不是一个有效的json字符串。那些“ee”和0破坏了语法,无法解析响应。我也对这两个位表示怀疑。我在fiddler显示的json响应中看到了它们,我不确定它们是什么意思。这导致它弹出两次undefined,这是正确的次数。team.description和team.id为空。如此接近,但还不正确。这返回了正确的数据。问题解决了,但为什么是“this”而不是“team”?
$。每个(teamArray,function(i,team){team.description}
您只有一个参数,这就是它无法工作的原因…请参阅更新的代码以将其与
团队一起使用
这会导致它弹出两次未定义,这是正确的次数。team.description和team.id为null。如此接近,但还不正确。这会返回正确的数据。问题已解决,但为什么“This”而不是“This”团队“?
$。每个(团队数组,函数(i,团队){team.description}
您只有一个参数,这就是为什么它不工作的原因…请参阅更新的代码以将其与
团队一起使用