How do I get field values?

-1

My code

$(document).ready(function(){ 

  $('.formulario').submit(function(e){ e.preventDefault(); 
  
  var inputId = $('.inputId').val();
  
  $.ajax({ method: "GET" 
 ,url:"localhost/api/32327ed48666154acb54810521d6f01e0d5de59e/movi‌​es/…"
 , dataType: 'json', 
  }).done(function(json) { 
  
    console.log(json);
    $('body').append(
      'ID do Genero: ' + json.genre.id);	
    }); 
    
  });
});

I'm trying to get the values from the genre field, but I'm not getting anyone could help me?

    
asked by anonymous 01.11.2017 / 13:32

2 answers

0

Next, suppose you want to catch the 'gender' within 'genre'.
To get values from a list (or json) inside another list, we must use a loop inside another loop ...
Example. Assuming your JSON is within 'items':

for(var i = 0 ; i < items.length; i++){
    var currentGenre = items[i]['genre'];
    for(var f = 0; f < currentGenre.length; f++){
        //Aqui vc consegue pegar os valores que estão dentro de genre
    }
}

Just a note. For this to work, what's inside genre also has to be a JSON object and not a string .... to transform strings into json use JSON.parse. a basic example of its use.

var currentGenre = JSON.parse(items[i]['genre']);

link
This link above has an explanation of how it works right

    
01.11.2017 / 13:41
0

In this example, we have a string that can be converted to a JSON array. Parse example:

var myJson = 
{ 
   id: "1",
   genre: "[ {\"id\":\"81\", \"genero\": \"comédia\"} ]" 
};

var genre = JSON.parse( myJson.genre );

for(var i=0; i<genre.length; i++)
{
   document.getElementById('divGenero').innerHTML += genre[i].genero;
}
<div id="divGenero"></div>
    
01.11.2017 / 13:59