1

I have retrieve JSON from my controller of asp.net mvc :

{"ja":
[
{"Name":"ABC1","PictureName1":"my image name1","ID":1},   
{"Name":"ABC2","PictureName2":"my image name2","ID":2},
.......
]}

In my view, I create one array in jquery :

var list_lastpage = [];

And I want to push all the element of my JSON to my new array that I just creat.

Could anyone tell me, how can I push and display the JSON(ja) to the array (list_lastpage)?

4

4 回答 4

1

Try this in the success handler of your AJAX request:

success: function(json) {
    list_lastpage.push(json.ja);
}

If you have multiple ja objects in your returned JSON, try this:

success: function(json) {
    $.each(json.ja, function(i, val) {
        list_lastpage.push(val);
    });
}
于 2012-05-21T09:09:08.977 回答
1

为了将ja数组中的所有项目推入 list_lastpage,请尝试以下操作:

for (var i = 0, length = ja.length; i < length; i++) {
  list_lastpage.push(ja[i]);
}

更新

我不确定您希望如何显示数组,但也许以下内容会有所帮助:

$.each(list_lastpage, function(i, val) {
  var div = $('<div></div>');
  div.attr('id', val.ID);
  div.attr('name', val.Name);
  div.val(val.PictureName);
  $('#containerId').append(div);
});
于 2012-05-21T09:10:24.267 回答
1

在您的 Ajax 调用成功后,循环遍历 Json 字符串并添加到数组中:

        success: function (ja) {
            $.each(jQuery.parseJSON(ja), function () {
                list_lastpage.push(this);
            });
于 2012-05-21T09:11:33.920 回答
1

除了其他答案之外,如果您想在视图中显示数组,您可以试试这个

 $(document).ready(function () {
    $.getJSON("/Home/userid", function (data) {
        var items = [];


        $.each(data, function (key, users) {
            items.push('<li id="' + users.UserId + '">' + users.UserName + '</li>');
        });

                $('<ul/>', {
                    'class': 'Users-list',
                    html: items.join('')
                }).appendTo('p');
           });
   });
 <p></p>
于 2012-05-21T10:45:34.850 回答