如何循环这个JSON?

时间:2017-06-13 01:58:22

标签: javascript php jquery json

我从控制器获得了这个JSON。

控制器:

$data = array ('pks_h' => $data_pks, 'pks_d' => $data_pks_d);
echo json_encode($data);

JQuery:

success     : function(data){
        $('#name').val(data.pks_h[0]['name']);

        alert(JSON.stringify(data.pks_d));
        //[{"no":"1","nomor":"19/0048-PKS/SCG-PM1","id_object":"B001","jumlah":"6000000","jumlah_sisa":"6000000","harga":"150","status":"1"},{"no":"2","nomor":"19/0048-PKS/SCG-PM1","id_object":"B009","jumlah":"15000000","jumlah_sisa":"15000000","harga":"200","status":"1"},{"no":"3","nomor":"19/0048-PKS/SCG-PM1","id_object":"B020","jumlah":"10000000","jumlah_sisa":"10000000","harga":"250","status":"1"}]

        $.each(data.pks_d, function(i, response){ alert(response.harga); });
    }

为什么alert(response.harga);不起作用?但是,将数据插入文本框的脚本正在运行。

1 个答案:

答案 0 :(得分:0)

它适用于我(我收到150,200,250的警报)。也许data.pks_d不是您的想法,或者您的浏览器可能阻止其他警报?



var data = {};
   data.pks_d = [{"no":"1","nomor":"19/0048-PKS/SCG-PM1","id_object":"B001","jumlah":"6000000","jumlah_sisa":"6000000","harga":"150","status":"1"},{"no":"2","nomor":"19/0048-PKS/SCG-PM1","id_object":"B009","jumlah":"15000000","jumlah_sisa":"15000000","harga":"200","status":"1"},{"no":"3","nomor":"19/0048-PKS/SCG-PM1","id_object":"B020","jumlah":"10000000","jumlah_sisa":"10000000","harga":"250","status":"1"}];

   $.each(data.pks_d, function(i, response){ alert(response.harga); });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;