JSON对象返回undefined?

时间:2016-08-12 02:31:38

标签: javascript json

这个数组。

var theObject = [{"id":"412","thn_akademik":"20152","id_prodi":"15301","kode_mk":"20038","tanggal":"23\/8\/2016","jam_1":"17:00\t","jam_2":"19:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"558","thn_akademik":"20152","id_prodi":"15301","kode_mk":"20053","tanggal":"25\/8\/2016","jam_1":"17:00\t","jam_2":"19:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"704","thn_akademik":"20152","id_prodi":"15301","kode_mk":"20052","tanggal":"30\/8\/2016","jam_1":"17:00\t","jam_2":"19:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"850","thn_akademik":"20152","id_prodi":"15301","kode_mk":"000047","tanggal":"31\/8\/2016","jam_1":"17:00\t","jam_2":"19:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"996","thn_akademik":"20152","id_prodi":"15301","kode_mk":"20050","tanggal":"23\/8\/2016","jam_1":"09:00\t","jam_2":"11:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"1142","thn_akademik":"20152","id_prodi":"15301","kode_mk":"20051","tanggal":"25\/8\/2016","jam_1":"09:00\t","jam_2":"11:00\t","npm":"07150100190","flag1":"0","flag2":"0"},{"id":"1288","thn_akademik":"20152","id_prodi":"15301","kode_mk":"000030","tanggal":"30\/8\/2016","jam_1":"09:00\t","jam_2":"11:00\t","npm":"07150100190","flag1":"0","flag2":"0"}] ;


for (var i = 0; i < theObject.length; i++) { 
    alert(theObject[i].kode_mk);
}

当我在fiddle上进行测试时,它会起作用。

但是当我尝试使用我的应用程序时,返回undefined。

任何人都知道是什么导致这种情况发生的?

1 个答案:

答案 0 :(得分:4)

这是 javascript 对象 JSON < / strong>对象。目前还不清楚你在问什么“ 但当我尝试使用我的应用程序时返回undefined。 ”。什么样的应用程序?

如果您的应用正在检索JSON,那么在尝试循环之前,您必须 解析 。 E.g

var json = JSON.parse(JSON_HERE); 

然后,您可以遍历该json对象并检索kode_mk

如果您有“应用”中的代码,请提供。告诉我们您的应用正在做什么以及如何获取JSON