Pedro
Pedro

Reputation: 1477

$.each Json Data

im having problems in getting the data inside my json file, it says "undefined". Im trying to get the data from the "key" parameter.

Json Structure:

{
   "list":[
      {
         "key":"12 ano",
         "value":"12 ano"
      },
      {
         "key":"12 ano administrativo",
         "value":"12 ano administrativo"
      }
   ]
}

Here is the code:

 $.getJSON('what.json', function(data){
        $.each(data, function(i, value){

         console.log(value.key);
      })
    }); 

Upvotes: 1

Views: 35

Answers (3)

Harpartap Singh Permar
Harpartap Singh Permar

Reputation: 289

Use This

$.getJSON( "ajax/test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
  alert(val);
  alert(key);
  });

Upvotes: -1

void
void

Reputation: 36703

Iterate inside data.list array

$.getJSON('what.json', function(data){
      $.each(data.list, function(i, value){
         console.log(value.key);
      })
}); 

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You need to iterate data.list, You are getting the error as your JSON doesn't have key at top level

$.each(data.list, function(i, value){
     console.log(value.key);
});

Upvotes: 1

Related Questions