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 Answers 4

1

In order to push all items in the ja array into the list_lastpage, try the following:

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

Update

I'm unsure how you want the array to be displayed, but perhaps the following can help:

$.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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, and could you tell me how to display this array?
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);
    });
}

3 Comments

Maybe he is looking for merging [] with array from ja? Simple push will add new array to list_lastpage.
@VisioN Thanks - now I re-read the OP that does seem more like what he's trying to do.
Thank Rory McCrossan, and could you tell me how to display the array?
1

In the success of your Ajax call, loop through the Json string and add to the array:

        success: function (ja) {
            $.each(jQuery.parseJSON(ja), function () {
                list_lastpage.push(this);
            });

1 Comment

If you expect json from server, there is no need in parsing the incoming data. JQuery does that job for you.
1

apart from the other answers if you want to display the array in your view you can try this

 $(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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.