1

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

而且我弹出警报框 6 次(应该是 2 次),每次都显示“未定义”而不是实际描述。

选择框本身有四个空选项。

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

我怎样才能解决这个问题?

4

2 回答 2

1

试试这个 - teamArray[0]应该只是teamArray

$.each(teamArray, function(i,team) {
    alert(team.description);
    $el.append($("<option></option>").attr("value", team.id).text(team.description));
});
于 2013-04-21T18:07:46.513 回答
0

现在,您正在循环 的键teamArray[0],因此有六个警报。循环过去teamArray。此外,$.each的回调需要indexInArray, valueOfElement. 也可以不通过 jQuery:

for(var i = 0; i < teamArray.length; i++) {
    var team = teamArray[i];
    ...
}
于 2013-04-21T18:24:44.700 回答