Error Scrolling a Json

2

Good afternoon guys, I have a question regarding 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 while trying to browse through the product list, it only loads 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 is a print:

Thanks for the help!

    
asked by anonymous 19.04.2017 / 22:30

1 answer

2

You have to concatenate the 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 a string:

$('#gridCategorias').html(data.result.lista.toString()); // mas vai vir com vírgulas entre os dados
    
19.04.2017 / 22:58