如何使用javascript在for循环中解析这个简单的json对象?

时间:2014-05-08 21:17:14

标签: javascript json parsing loops

var playlist = JSON.parse('{"VideoID" : {"VideoTitle" : "the title of the video"} }');

for(var video in playlist){
document.write(video + " "+ video.title +"<br>");
}

输出为:VideoID undefined

我希望将其视为:VideoID视频标题

2 个答案:

答案 0 :(得分:2)

正确用法是:

 for(var video in playlist){
    document.write(video + " "+ playlist[video].VideoTitle +"<br>");
 }

答案 1 :(得分:0)

这应该有效,

var playlist = JSON.parse('{"VideoID" : {"VideoTitle" : "the title of the video"} }');

document.write(playlist.VideoID.VideoTitle);