How do I get the field values?

Asked

Viewed 26 times

-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);	
    }); 
    
  });
});

inserir a descrição da imagem aqui

I’m trying to get the field values Genre, but I’m not getting anyone could help me?

  • 5

    Maycon, instead of the image post the code so we can help you.

  • Welcome Maycon Benito, when you ask about a problem in your code, you’ll get better answers if you give people code that they can use to reproduce the problem. Read this post https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485

2 answers

0

Next, suppose you want to take the 'genre' inside 'Enre'.
For you to take values from a list (or json) within another list, we should use a loop within another loop...
Example. Suppose your JSON is inside '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 one observation. 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']);

https://www.w3schools.com/js/js_json_parse.asp
On this link above is an explanation of how it works right

0

In this example, we have a string that can be converted into a JSON array. Example of parse:

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.