Error Browsing a Json

Asked

Viewed 44 times

2

Good afternoon, you guys, I have a question about json,

i have the following json :

{
  "jsonrpc":"2.0",
  "result":{
    "nr":26,
    "lista":[
      {
        "codigo":"2",
        "nome":"Pratos Quentes"
      },
      {
        "codigo":"3",
        "nome":"Sobremesas"
      },
      {
        "codigo":"4",
        "nome":"Bebidas Nao Alcoolicas"
      },
      {
        "codigo":"7",
        "nome":"Cocktails"
      },
      {
        "codigo":"10",
        "nome":"Cafes"
      },
      {
        "codigo":"11",
        "nome":"Consummes"
      },
      {
        "codigo":"12",
        "nome":"Porções"
      },
      {
        "codigo":"13",
        "nome":"Chocolates"
      }
    ]
  },
  "id":138827
}

and when trying to go through the list of products , only loads only the last product this is the code I’m using to go through:

<script type="text/javascript">
     var caminho = "http://brunofejesus.pe.hu/categorias.json";
     $.getJSON(caminho, function(data) {

         for(i in data.result.lista){
            $('#gridCategorias').html(data.result.lista[i].nome);
         }
     });
  </script>

here a print :

inserir a descrição da imagem aqui

thanks in advance for the help!

1 answer

2


you have to concatenate html:

var html = '';
$.getJSON(caminho, function(data) {

     for(i in data.result.lista){
        html += '<p>'+data.result.lista[i].nome+'</p>';
     }
     $('#gridCategorias').html(html);
 });

or turn your array into string:

$('#gridCategorias').html(data.result.lista.toString()); // mas vai vir com vírgulas entre os dados
  • Thanks Douglas for the help ! it worked vlw

Browser other questions tagged

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