Javascript – Jquery Ajax Json data Undefined

ajaxjavascriptjqueryjsonundefined

I have a URL that I have been provided with that returns data in JSON. I'm trying to display the value of each of this objects using jQuery AJAX call. I keep getting 'undefined'. What am I doing wrong?

The data is as follows:

[
    [
        {
            "label": "First",
            "value": "XXXXXX"
        },
        {
            "label": "Second",
            "value": "XXXXXX"
        },
        {
            "label": "Third",
            "value": "XXXXXX"
        },
        {
            "label": "Fourth",
            "value": "XXXXXX XXX"
        },
        {
            "label": "Fifth",
            "value": "XXXXXX"
        },
        {
            "label": "Sixth",
            "value": "XXXXXX"
        }
    ]
]

My jQuery is as follows:

$(document).ready(function(){
    $.ajax({
        type: 'GET',
        url: 'http://url',
        dataType: 'json',
        success: function(data) {
            var items = [];
            $.each(data, function() {
                var label = items.label;
                var value = items.value;
                $('#results').append('<p>' + label + '</p>' + '<p>' + value + '</p>');
            });
        }
    });
    return false;
});

My html is:

<div id="results">
</div>

Best Solution

The data in your JSON file is stored as an array within an array. Try this:

$(document).ready(function() {
 $.ajax({
     type: 'GET',
     url: 'http://url',
     dataType: 'json',
     success: function(data) {
         for (var i = 0; i < data.length; i++) {
             var items = data[i];

             $('#results').append('<div class="block"></div>');
             var $block = $('#results').find('.block').last();

             for (var j = 0; j < items.length; j++) {
                 var item = items[j];
                 var label = item.label;
                 var value = item.value;
                 $block.append('<p>' + label + '</p>' + '<p>' + value + '</p>');
             }
         }
     }
 });
 return false;
 });