how to read json data for multiple rows in Jquery + wordpress

i’m getting following data from json

Return Data:

Read More
{
    "items": [{
        "name": "abc",
        "meta_value": "3020"
    }, {
        "name": "xyz",
        "meta_value": "3020"
    }]
}

i’m using this to encode data echo json_encode(array('items' => $data));

and following to decode and print data from json but it is giving error of

“Uncaught TypeError: Cannot read property ‘length’ of undefined”

$.each(data.items, function(i, item) {
    $('#dobsondev-ajax-table').append('<tr><td>' + data.name + '</td><td>' + data.name + '</td><td>' + data.meta_value + '</td><td>' + data.name + '</td></tr>');

});

Related posts

2 comments

  1. Try this. You can use item argument inside the loop in the function or you can also try data.items[i].

    $.each(data.items, function(i, item) {
        $('#dobsondev-ajax-table').append('<tr><td>' + item.name + '</td><td>' + item.name + '</td><td>' + item.meta_value + '</td><td>' + item.name + '</td></tr>');
        //alert(this.name);
    });
    
  2. You can’t call the first item in the array directly with [0]. The first thing you have to do is retrieving the items from your returned data, like this:

    var items = data["items"];
    

    This will give you the two item objects of your returned data. From there you can retrieve the two item objects like this:

    var item = items[0];
    

    Or in a loop:

    $.each(items, function(i, item) {
        // Your code
    }
    

Comments are closed.