如何使用javascript解析嵌套的jsonp对象?

时间:2016-10-04 22:22:09

标签: javascript jquery json parsing jsonp

我有一个有效的jsonp,如下所示。我正在尝试解析它,但我不知道如何访问视频元素!任何人都可以告诉我如何在循环中引用这些元素(来源,拇指,标题)吗?谢谢

有效的jsonp:

{
    "categories": [{
        "name": "october list",
        "videos": [{
                "sources": [
                    "http://www.somesite.com.com/test.m3u8"
                ],
                "thumb": "http://www.somesite.com.com/1.jpg",
                "title": "title of test",
                "subtitle": "",
                "description": ""
            }, {
                "sources": [
                    "http://www.somesite.com/test2.m3u8"
                ],
                "thumb": "http://www.somesite.com/2.jpg",
                "title": "test2",
                "subtitle": "",
                "description": ""
            }

        ]
    }]
}

的javascript:

 $.get("http://www.someapisite.com/test.php",
        {

          dataType: "jsonp"
        },

        function(data,status){

 var json = $.parseJSON(data);

// then loop the single items
    for(i in json)
    {
       // create HTML code
       var div = "<div class=\"image\">\n" + 
       "<a href=\"javascript:dofunction('./test.php?title=" + json[i].title + "&TargetUrl=http://somesite.com/" + json[i].sources + "')\">\n" +
       "<img src=\""+ json[i].thumb +"\" alt=\"..\" />\n" +
       "</a>\n" +
       "</div>\n";
       // append it inside <body> tag.
         $("#myDiv").append(div);
    }

        });

1 个答案:

答案 0 :(得分:0)

您似乎想要为每个类别迭代视频数组。

使用javascript forEach

json.categories.forEach(function(category, index, array) {
category.videos.forEach(function(videoDetail, index, array) {
    <img src=\"" videoDetail.thumb \"" />
    <h3>videoDetail.title</h3>
 });
});