简体   繁体   English

如何遍历此JSON字符串?

[英]How to loop through this JSON string?

I have a script that post a variable and get a JSON as result. 我有一个脚本,可以发布变量并得到JSON作为结果。 This is the script: 这是脚本:

$.post('<?php echo site_url('Admin/get_memo_by_surat'); ?>',{data:id},function(result){
   alert(result);
});

When I do alert if the script success post the variable, I get this JSON : 当我确实通知脚本是否成功发布变量时,我得到以下JSON:

[{"no_dokumen":"19\/0001-1\/SCG","dari":"PPC","pic":"77973035","kepada":"PPC","instruksi":"Mas Dwi tolong hps","tanggal_dist":"2017-03-27 13:31:53","tanggal_proses":"0000-00-00 00:00:00"},{"no_dokumen":"19\/0002-1\/SCG","dari":"PPC","pic":"77973035","kepada":"PMD","instruksi":"Test to PMD","tanggal_dist":"2017-03-27 13:32:15","tanggal_proses":"0000-00-00 00:00:00"}]

I've tried using $.each , but it show nothing. 我试过使用$.each ,但什么也没显示。

$.each(result, function(i,response){ alert(response.no_dokumen); });

So, how do I loop through this? 那么,我该如何循环呢? I'd like to represent this data in a table by the way. 我想用表格来表示这些数据。

this result variable contain json data but string. 此结果变量包含json数据,但包含字符串。 you just add the new line: 您只需添加新行:

var data = JSON.parse(result);

and then try, 然后尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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