1

How can I get all the data from the JSON array objects with jquery?
I have tried before but my code only can get the data from the JSON object.

This is my json file student.json :

{"status":true,
  "offset":0,
  "limit":25,
  "total":2,
  "data":[
    { "id":231,
      "title":"mytitle1",
      "content":"myconten1",
      "created_at":"2017-07-10 03:56:32",
      "updated_at":"2017-07-10 03:56:32"
    },{ "id":230,
        "title":"mytitle2",
        "content":"mycontent2",
        "created_at":"2017-07-10 03:56:06",
        "updated_at":"2017-07-10 03:56:06"
    }]}

My js script :

<script>
    $(document).ready(function(){
        $(function (){
            var $orders = $('#orders');
            $.ajax({
                type: 'GET',
                url: 'json/student.json',
                success: function(data) {
                    console.log('success', data);
                    $.each(data, function(i, dataentry){
                        $orders.append('<li>dataid: '+dataentry.id+'</li>');
                    });
                }
            });
        });

    });
</script>
11
  • you mean your ajax call fail? Commented Aug 27, 2017 at 12:50
  • You should read up on the difference between JSON, an object literal in JS and an Array. Commented Aug 27, 2017 at 12:51
  • 2
    i think it shoild be : data.data[0].id Commented Aug 27, 2017 at 12:52
  • i can grab the data with ajax and show it in the console log only, but when i try to show in the html data undifiend @artgb Commented Aug 27, 2017 at 12:55
  • 1
    to add to my previous comment: if you load a json via jquery ajax, it automatically will parse it to a object literal so it can be used directly in your script via dot notation. Just to make the wording more clear. Commented Aug 27, 2017 at 13:05

4 Answers 4

5

So first, you don't need to write this:

$(document).ready(function(){  
             $(function (){

Because $(function() ( w/o a space ) is a short for $(document).ready(function().

Regarding your issue - I believe that data is the entire JSON, so you need to extract data.data, so I would write this:

$(function (){
    var $orders = $('#orders');
    $.ajax({
        type: 'GET',
        url: 'json/student.json',
        success: function(response) {      // <= this is the change
            var data = response.data;      // <= going inside the data itself
            $.each(data, function(i, data){
                $orders.append('<li>dataid: '+data.id+'</li>');
            });

        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

finally i can show the data, with your suggestion, thanks alot
Awesome, if you can just approve the answer - that would be best ;)
you're so amazing bro :)
2

In your success function, the data received is the actual data response of your ajax call.

You should see it in your console.log statement with all properties like offset, limit, total etc.

Anyway, you're trying to loop through the whole object, not the data property inside the response, which is actually the array you probably want to loop through. You won't get any errors because $.each can loop through object literals as well as through arrays.

Here's how it should look like (i adjusted the variable names to make it clearer):

success: function(response) {
    $.each(response.data, function(i, dataEntry){     // or response['data']
         $orders.append('<li>dataid: '+dataEntry.id+'</li>');
    });
}

Hope it helps.

Comments

0

If your ajax call is successful, then :

  • function(data) : this data is the base object returned from server, in your case its not the data property.

  • now the data in your json is an array.

so, instead of using foreach on root object, use it on data.data. hope it will help.

Comments

0

Here you go with an example solution https://jsfiddle.net/xydqLLdb/

var response = {"status":true,
  "offset":0,
  "limit":25,
  "total":2,
  "data":[
    { "id":231,
      "title":"mytitle1",
      "content":"myconten1",
      "created_at":"2017-07-10 03:56:32",
      "updated_at":"2017-07-10 03:56:32"
    },{ "id":230,
        "title":"mytitle2",
        "content":"mycontent2",
        "created_at":"2017-07-10 03:56:06",
        "updated_at":"2017-07-10 03:56:06"
    }]};
    
$.each(response.data, function(i, data){
  $('ul').append('<li>dataid: ' + data.id + '</li>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
</ul>

Based on your scenario

$(document).ready(function(){  
 $(function (){
  var $orders = $('#orders');
  $.ajax({
    type: 'GET',
    url: 'json/student.json',
    success: function(response) {
      $.each(response.data, function(i, data){
        $orders.append('<li>dataid: ' + data.id + '</li>');
      });
    }
  });
});
});

You need to loop through the data key with the repsonse data.

Hope this will help you.

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.