简体   繁体   中英

I can not access data from a JSON object

I have a problem I can not access the data of a JSON object, it returns Object Object when using "each"

function NumeroConos(id_cono) {
  var ParamObjSend = {
    'id_cono' :id_cono,
  };

  $.ajax({
    type: "POST",
    url: "<?= base_url() ?>AgregarOTController/NumeroConos",
    data: ParamObjSend,
    dataType: 'json',
    success: function(objView) {
      var items = objView.NumeroConos[0].numero_conos.split(';');

      $.each(items, function (ind, elem) {
        var option         = document.createElement('option');
        option.value       = $(this);
        option.textContent = $(this);
        $('#numero_conos').append(option);
      });
    }
  });
}

输出 - 对象对象

JSON

{
  "success": true,
  "NumeroConos": [
    {
      "id_cono": "1",
      "descripcion_cono": "sdfasdf",
      "color_cono": "Rojo",
      "numero_conos": "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50",
      "token": "GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy",
      "estado": "0"
    }
  ]
}

The solution, suggested by ajai Jothi in the comment, solves your issue.

Instead, if you want to take advantage of how jQuery Creates New Elements on the fly you may write in a reduced format:

$.each(items, function (ind, elem) {
    $('#numero_conos').append($('<option/>', {value: elem, text: elem}));
});

The example:

 var objView = {"success":true,"NumeroConos":[{"id_cono":"1","descripcion_cono":"sdfasdf","color_cono":"Rojo","numero_conos":"1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50","token":"GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy","estado":"0"}]}; var items = objView.NumeroConos[0].numero_conos.split(';'); $.each(items, function (ind, elem) { $('#numero_conos').append($('<option/>', {value: elem, text: elem})); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="numero_conos"> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM