How can I traverse a json array, get its values to show them in the view?

1
function nuevaPartidaVenta() {
    var ticket = $('#ticket').val();
    if (ticket != null && validaFormPartidaVenta()) {
        //$(".agregar_partida_catalogo").hide();
        var style_edit = 'style="width: 100%;"';
        var url = '<?php echo base_url(); ?>facturas/Facturas/getPartidaFactura'
        $.ajax({
            type: 'POST',
            url: url,
            data: 've_id_venta=' + ticket,
            dataType: 'json',
            success: function(resp) {
                var valores = JSON.stringify(resp);
                console.log(valores);
            }
        });
    }
}

Controller

function getPartidaFactura()
{
    $ve_id_venta = $this->input->post('ve_id_venta');
    $partidas = $this->ventasModel->getPartidasVenta($ve_id_venta);
    echo json_encode($partidas);
}

Output by console

[  
   {  
      "id_partida_venta":"38",
      "pa_cantidad":"1",
      "pa_unidad":"Caj‌​a ",
      "pa_descripcion":"Duvalin",
      "pa_precio":"200",
      "pa_importe":‌​"200",
      "pa_descArt":"‌​0",
      "pa_status":"1",
      "‌​pa_fecha_registro":"‌​2017-02-10",
      "pa_id_v‌​enta":"14",
      "pa_id_pr‌​oducto":"190",
      "tipoD‌​esc":"null",
      "impDesc‌​Art":"0",
      "descArt":"‌​0",
      "pa_descripcion_p‌​artida":"",
      "pa_clave‌​_producto":"DV500"
   }
]

[  
   {  
      "id_partida_venta":"39",
      "pa_cantidad":"1",
      "pa_unidad":"Caj‌​a ",
      "pa_descripcion":"Paginas Web",
      "pa_precio":"1800",
      "pa_importe":"1800",
      "pa_descArt":"0"      ‌​,
      "pa_status":"1",
      "pa‌​_fecha_registro":"20‌​17-02-10",
      "pa_id_ven‌​ta":"14",
      "pa_id_prod‌​ucto":"187",
      "tipoDes‌​c":"null",
      "impDescAr‌​t":"0",
      "descArt":"0"      ‌​,
      "pa_descripcion_par‌​tida":"",
      "pa_clave_p‌​roducto":"tai1"
   }
]
    
asked by Cesar Vieyra 16.02.2017 в 00:59
source

1 answer

1

The result you receive in the jQuery.ajax at the time of defining dataType to json automatically converts it to a JSON object.

When using JSON.stringify() you are converting an object to a JSON string. In your case it is not necessary.

For example:

var cadenaJSON = '[{"value": "New", "onclick": "CreateNewDoc()"},  {"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]';

var menus = JSON.parse(cadenaJSON); // objeto JSON

// A modo de ejemplo, recorrer con un for
for(var indice in menus){
  console.log(menus[indice].value);
}

// Mostrar el objeto como cadena
console.log(JSON.stringify(menus));

Another simplified way is to use jQuery.getJSON() , which makes a request with the verb GET . Using jQuery.each() to scroll through the result.

For example:

(function() {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
  })
    .done(function( data ) {
      $.each( data.items, function( i, item ) {
        $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
        if ( i === 3 ) {
          return false;
        }
      });
    });
})();
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<div id="images"></div>
    
answered by 16.02.2017 в 02:27