0

I have a valid jsonp as follows . I am trying to parse it but i don't know how to access videos elements! Could any one tell me how to reference those elements(sources,thumb,title) inside for loop ?Thanks

valid 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);
    }

        });
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • That is *not* JSONP. – Bergi Oct 04 '16 at 23:57
  • You don't have to call `$.parseJSON`. `$.ajax`/`$.get`/`$.getJSON` already does that for you (and in case of actual JSONP, you never get to see a string anyway) – Bergi Oct 04 '16 at 23:58
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/1048572)? – Bergi Oct 04 '16 at 23:59
  • Thanks for replies. @Bergi i still don't get any data out inside divs after removing parseJson and use data instead! How to fix it ? – user1788736 Oct 05 '16 at 00:05

1 Answers1

0

It seems you want to iterate over the videos array for each category.

Using javascript forEach

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