View JSON object

Asked

Viewed 1,124 times

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?

  • 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, try to post the code with the whole function, the this of this.qtd refers to what? we also need to know where that came from data. This information will be useful so we can help you.

  • Caique, I posted the full code.

5 answers

1

You can use the method foreach() to traverse the arrays of his objeto json.

var json = [
  {
    "nome": "Meu Nome",
    "amigos": [
      {
        "amigo": "João",
        "idade": 20
      }            
    ]
  }
];

json.forEach(function(item) {
    item.amigos.forEach(function(item) {
        console.log(item.amigo);
    });
});

  • 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? Thank you.

  • Edit your question and include the code with that for and how it is called, so we can better understand your question.

  • Caique, I’m sorry I’m a beginner in JSON and really did not know how to express myself. I edited my post. If you can help me thank.

0

Try to do it that way:

for (i = 0; i < this.qtd; i++){
                this.retorno += 'Nome: ' + data.json[i].nome;
        this.retorno += 'Amigo:' + JSON.stringify(data.json[i].amigos[i].amigo);
}
  • will return only the name of friends

  • Unfortunately it didn’t work that way.

  • [link]var json = [ { "name": "My Name", "friends": [ { "friend": "John", "age": 20 } ] } ] Undefined for length (i = 0; i < json.; i++){ 'Name: ' + json[i]. name; 'Friend:' + JSON.stringify(json[i].friends[i].friend); } "Friend:"John""

0

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

0

I managed to solve as follows:

$.getJSON('www.site.com.br/arquivo.json', function(data){
		
	this.qtd = data.json.length;
	this.retorno = '';

	for(i in data.json){
    	this.retorno += 'Nome: ' + data.json[i].nome + '<br />';

        for (j in data.json[i].amigos){
            this.retorno += 'TXT: ' + data.json[i].amigos[j].amigo + '<br />';
        }    

        this.retorno += '<br />';
    }						

	$('#resultado').html(this.retorno);
});

Thank you all for your help.

-3

var json = [
  {
    "nome": "Meu Nome",
    "amigos": [
      {
        "amigo": "João",
        "idade": 20
      }            
    ]
  }
];

json.forEach(function(item) {
    item.amigos.forEach(function(item) {
        console.log(item.amigo);
    });
});

  • 3

    It seems to be exactly the same code of this answer. What was your intention in answering?

Browser other questions tagged

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