0
I have the following JSON:
"json": [
{
"nome": "Meu Nome",
"amigos": [
{
"amigo": "João",
"idade": 20
}
]
}
]
I also have the following:
this.qtd = data.json.length;
for (i = 0; i < this.qtd; i++){
this.retorno += 'Nome: ' + data.json[i].nome;
this.retorno += 'Amigo:' + JSON.stringify(data.json[i].amigos);
}
Would you like to know how to display on the screen the name only the name of the friend? Because when I do "JSON.stringify()", it returns with keys, quotes, etc.
How can I just take the friend’s name?
Below the full code:
function dados(){
var qtd;
var retorno;
json.prototype.resgatarValores = function(){
$('#resultado').html('loading...');
$.getJSON('http://www.site.com/arquivo.json', function(data){
this.qtd = data.json.length;
this.retorno = '';
for (i = 0; i < this.qtd; i++){
this.retorno += 'Nome: ' + data.json[i].nome + '<br />';
this.retorno += 'Amigo: ' + JSON.stringify(data.json[i].amigo) + '<br />';
}
$('#resultado').html(this.retorno);
});
}
}
var obj = new dados();
obj.resgatarValores();
Of that form?
– BrTkCa
Lucas, that would be it. The problem is that I already have the code like this: for (i = 0; i < this.Qtd; i++){ this.return += 'Name: ' + data.json[i]. name + '<br />'; } How can I insert into this for? Can you help me? Thanks.
– Edson Júnior
Edson, try to post the code with the whole function, the
thisofthis.qtdrefers to what? we also need to know where that came fromdata. This information will be useful so we can help you.– Caique Romero
Caique, I posted the full code.
– Edson Júnior