0

I am trying to parse JSON data like this:

   var baseUrl = 'https://api.themoviedb.org/3/movie/'
    var movieID = '550'
    var detailUrl = '&append_to_response=releases,trailers,credits&callback=?'
    var apiKey = '?api_key=Removed_For_Privacy'

The above url with the api key include returns this result:

?({
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
    "homepage": "",
    "id": 550,
    "imdb_id": "tt0137523",
    "original_title": "Fight Club",
    "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
    "popularity": "10.2188172784825",
    "poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
    "production_companies": [
        {
            "name": "20th Century Fox",
            "id": 25
        },
        {
            "name": "Fox 2000 Pictures",
            "id": 711
        },
        {
            "name": "Regency Enterprises",
            "id": 508
        }
    ],
    "production_countries": [
        {
            "iso_3166_1": "DE",
            "name": "Germany"
        },
        {
            "iso_3166_1": "US",
            "name": "United States of America"
        }
    ],
    "release_date": "1999-10-14",
    "revenue": 100853753,
    "runtime": 139,
    "spoken_languages": [
        {
            "iso_639_1": "en",
            "name": "English"
        }
    ],
    "status": "Released",
    "tagline": "How much can you know about yourself if you've never been in a fight?",
    "title": "Fight Club",
    "vote_average": 7.6,
    "vote_count": 2787,
    "releases": {
        "countries": [
            {
                "iso_3166_1": "US",
                "certification": "R",
                "release_date": "1999-10-14"
            },
            {
                "iso_3166_1": "DE",
                "certification": "18",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "GB",
                "certification": "18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "FR",
                "certification": "16",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "TR",
                "certification": "",
                "release_date": "1999-12-10"
            },
            {
                "iso_3166_1": "BR",
                "certification": "feibris",
                "release_date": "1999-07-12"
            },
            {
                "iso_3166_1": "FI",
                "certification": "K-18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "BG",
                "certification": "c",
                "release_date": "2012-08-28"
            },
            {
                "iso_3166_1": "IT",
                "certification": "VM14",
                "release_date": "1999-10-29"
            }
        ]
    },
    "trailers": {
        "quicktime": [],
        "youtube": [
            {
                "name": "Trailer 1",
                "size": "HD",
                "source": "SUXWAEX2jlg",
                "type": "Trailer"
            }
        ]
    },
    "credits": {
        "cast": [
            {
                "id": 819,
                "name": "Edward Norton",
                "character": "The Narrator",
                "order": 0,
                "cast_id": 4,
                "profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
            },
            {
                "id": 287,
                "name": "Brad Pitt",
                "character": "Tyler Durden",
                "order": 1,
                "cast_id": 5,
                "profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
            },
            {
                "id": 1283,
                "name": "Helena Bonham Carter",
                "character": "Marla Singer",
                "order": 2,
                "cast_id": 6,
                "profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
            },
            {
                "id": 7470,
                "name": "Meat Loaf",
                "character": "Robert 'Bob' Paulson",
                "order": 3,
                "cast_id": 7,
                "profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
            },
            {
                "id": 7499,
                "name": "Jared Leto",
                "character": "Angel Face",
                "order": 4,
                "cast_id": 30,
                "profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
            },
            {
                "id": 7471,
                "name": "Zach Grenier",
                "character": "Richard Chesler",
                "order": 5,
                "cast_id": 31,
                "profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
            },
            {
                "id": 7497,
                "name": "Holt McCallany",
                "character": "The Mechanic",
                "order": 6,
                "cast_id": 32,
                "profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
            },
            {
                "id": 7498,
                "name": "Eion Bailey",
                "character": "Ricky",
                "order": 7,
                "cast_id": 33,
                "profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
            }
        ],
        "crew": [
            {
                "id": 7469,
                "name": "Jim Uhls",
                "department": "Writing",
                "job": "Author",
                "profile_path": null
            },
            {
                "id": 7474,
                "name": "Ross Grayson Bell",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 7475,
                "name": "Ceán Chaffin",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 1254,
                "name": "Art Linson",
                "department": "Production",
                "job": "Producer",
                "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
            },
            {
                "id": 7477,
                "name": "John King",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7478,
                "name": "Michael Simpson",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7479,
                "name": "Jeff Cronenweth",
                "department": "Camera",
                "job": "Director of Photography",
                "profile_path": null
            },
            {
                "id": 7480,
                "name": "James Haygood",
                "department": "Editing",
                "job": "Editor",
                "profile_path": null
            },
            {
                "id": 7481,
                "name": "Laray Mayfield",
                "department": "Production",
                "job": "Casting",
                "profile_path": null
            },
            {
                "id": 1303,
                "name": "Alex McDowell",
                "department": "Art",
                "job": "Production Design",
                "profile_path": null
            },
            {
                "id": 7763,
                "name": "Ren Klyce",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7764,
                "name": "Richard Hymns",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7467,
                "name": "David Fincher",
                "department": "Directing",
                "job": "Director",
                "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
            },
            {
                "id": 7468,
                "name": "Chuck Palahniuk",
                "department": "Writing",
                "job": "Novel",
                "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
            }
        ]
    }
})

I use this to parse it, however i have no luck

$(document).ready(function() {

  $.ajax({
    url: baseUrl + movieID +apiKey +detailUrl,
    dataType: "jsonp",
    success: getGenres,
  });
});


function getGenres(data) {
var entries = data
genre = 0,
genre_list = '';


for (genre = 0; genre < entries.genres.name.length; genre++) {
    genre_list.push(entries.genres.name[genre]);
}

    document.getElementById('Genres').innerHTML = genre_list.join(', ');

Please Help

cookie monster
  • 10,671
  • 4
  • 31
  • 45
Craig
  • 133
  • 1
  • 13
  • do you get an error message? and does that first question mark appear in the actual returned jsonp. – Patrick Evans Jan 25 '14 at 02:39
  • *"however i have no luck"* means what exactly? Could you please provide a better description of the problem, otherwise it's really difficult to help you. Is `entries.genres.name` actually an array? I think the problem is not so much that you cannot *parse* the response, but that you don't know how to access the properties of the resulting object. – Felix Kling Jan 25 '14 at 02:40
  • @PatrickEvans unfortunately no, i did not get error message which could point me in the right direction. Everything was copied over as it was retured. – Craig Jan 25 '14 at 02:46
  • @FelixKling It appears i am not properly accessing the data as i get no result returned – Craig Jan 25 '14 at 02:47
  • well if the return jsonp code begins with the `?` it will not run as its not a function name, i suspect your jquery call is not replacing the `callback=?` part or your url with a random generated function name like it is supposed to. – Patrick Evans Jan 25 '14 at 02:48
  • 1
    @PatrickEvans: I'm betting that the response shown in the question is coming from OP typing the URL into the browser, so the API things `?` should actually be the function name. – cookie monster Jan 25 '14 at 02:51
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Jan 25 '14 at 02:55
  • Btw, [if I try to do what you are trying to do](http://jsfiddle.net/Eh276/), I get the error `Uncaught TypeError: Cannot read property 'length' of undefined`. Didn't you notice that? – Felix Kling Jan 25 '14 at 02:59
  • @FelixKling unfortunately i never caught it – Craig Jan 25 '14 at 03:04

2 Answers2

1

entries.genres is an Array. It has no .name property. You should be getting an error in your browser's developer console for accessing .length of undefined.

{
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
 ...
 }

So you need to iterate entries.genres, then push the .name of the current genre if that's what you want.

for (genre = 0; genre < entries.genres.length; genre++) {
    genre_list.push(entries.genres[genre].name);
}


On a different note, you have two implicit globals.

var entries = data
genre = 0,
genre_list = '';

By forgetting the comma after var entries = data, the next two lines will implicitly create global variables since they're not part of the var statement.

That's why I always use leading commas for variable declarations. Makes it obvious when a comma is missing.

var entries = data
,   genre = 0
,   genre_list = '';
cookie monster
  • 10,671
  • 4
  • 31
  • 45
  • I tried your suggestions, however no results. maybe i'm missing something could you check? http://jsfiddle.net/Eh276/1/ – Craig Jan 25 '14 at 03:09
  • 1
    @Craig: You really should learn how to use the console: `Uncaught ReferenceError: data is not defined`. – Felix Kling Jan 25 '14 at 03:17
  • @Craig: You need to take the parts of the code that I corrected, and insert them into your original code. You can't just take fragments of code and expect them to run. – cookie monster Jan 25 '14 at 03:27
-2

test.php

   <?php
        echo 'getGenres({
    "adult": false,
    "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
    "belongs_to_collection": null,
    "budget": 63000000,
    "genres": [
        {
            "id": 28,
            "name": "Action"
        },
        {
            "id": 18,
            "name": "Drama"
        },
        {
            "id": 53,
            "name": "Thriller"
        }
    ],
    "homepage": "",
    "id": 550,
    "imdb_id": "tt0137523",
    "original_title": "Fight Club",
    "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
    "popularity": "10.2188172784825",
    "poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
    "production_companies": [
        {
            "name": "20th Century Fox",
            "id": 25
        },
        {
            "name": "Fox 2000 Pictures",
            "id": 711
        },
        {
            "name": "Regency Enterprises",
            "id": 508
        }
    ],
    "production_countries": [
        {
            "iso_3166_1": "DE",
            "name": "Germany"
        },
        {
            "iso_3166_1": "US",
            "name": "United States of America"
        }
    ],
    "release_date": "1999-10-14",
    "revenue": 100853753,
    "runtime": 139,
    "spoken_languages": [
        {
            "iso_639_1": "en",
            "name": "English"
        }
    ],
    "status": "Released",
    "tagline": "How much can you know about yourself if youve never been in a fight?",
    "title": "Fight Club",
    "vote_average": 7.6,
    "vote_count": 2787,
    "releases": {
        "countries": [
            {
                "iso_3166_1": "US",
                "certification": "R",
                "release_date": "1999-10-14"
            },
            {
                "iso_3166_1": "DE",
                "certification": "18",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "GB",
                "certification": "18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "FR",
                "certification": "16",
                "release_date": "1999-11-10"
            },
            {
                "iso_3166_1": "TR",
                "certification": "",
                "release_date": "1999-12-10"
            },
            {
                "iso_3166_1": "BR",
                "certification": "feibris",
                "release_date": "1999-07-12"
            },
            {
                "iso_3166_1": "FI",
                "certification": "K-18",
                "release_date": "1999-11-12"
            },
            {
                "iso_3166_1": "BG",
                "certification": "c",
                "release_date": "2012-08-28"
            },
            {
                "iso_3166_1": "IT",
                "certification": "VM14",
                "release_date": "1999-10-29"
            }
        ]
    },
    "trailers": {
        "quicktime": [],
        "youtube": [
            {
                "name": "Trailer 1",
                "size": "HD",
                "source": "SUXWAEX2jlg",
                "type": "Trailer"
            }
        ]
    },
    "credits": {
        "cast": [
            {
                "id": 819,
                "name": "Edward Norton",
                "character": "The Narrator",
                "order": 0,
                "cast_id": 4,
                "profile_path": "/iUiePUAQKN4GY6jorH9m23cbVli.jpg"
            },
            {
                "id": 287,
                "name": "Brad Pitt",
                "character": "Tyler Durden",
                "order": 1,
                "cast_id": 5,
                "profile_path": "/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
            },
            {
                "id": 1283,
                "name": "Helena Bonham Carter",
                "character": "Marla Singer",
                "order": 2,
                "cast_id": 6,
                "profile_path": "/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
            },
            {
                "id": 7470,
                "name": "Meat Loaf",
                "character": "Robert Bob Paulson",
                "order": 3,
                "cast_id": 7,
                "profile_path": "/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
            },
            {
                "id": 7499,
                "name": "Jared Leto",
                "character": "Angel Face",
                "order": 4,
                "cast_id": 30,
                "profile_path": "/msugySeTCyCmlRWtyB6sMixTQYY.jpg"
            },
            {
                "id": 7471,
                "name": "Zach Grenier",
                "character": "Richard Chesler",
                "order": 5,
                "cast_id": 31,
                "profile_path": "/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
            },
            {
                "id": 7497,
                "name": "Holt McCallany",
                "character": "The Mechanic",
                "order": 6,
                "cast_id": 32,
                "profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
            },
            {
                "id": 7498,
                "name": "Eion Bailey",
                "character": "Ricky",
                "order": 7,
                "cast_id": 33,
                "profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
            }
        ],
        "crew": [
            {
                "id": 7469,
                "name": "Jim Uhls",
                "department": "Writing",
                "job": "Author",
                "profile_path": null
            },
            {
                "id": 7474,
                "name": "Ross Grayson Bell",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 7475,
                "name": "Ceán Chaffin",
                "department": "Production",
                "job": "Producer",
                "profile_path": null
            },
            {
                "id": 1254,
                "name": "Art Linson",
                "department": "Production",
                "job": "Producer",
                "profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
            },
            {
                "id": 7477,
                "name": "John King",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7478,
                "name": "Michael Simpson",
                "department": "Sound",
                "job": "Original Music Composer",
                "profile_path": null
            },
            {
                "id": 7479,
                "name": "Jeff Cronenweth",
                "department": "Camera",
                "job": "Director of Photography",
                "profile_path": null
            },
            {
                "id": 7480,
                "name": "James Haygood",
                "department": "Editing",
                "job": "Editor",
                "profile_path": null
            },
            {
                "id": 7481,
                "name": "Laray Mayfield",
                "department": "Production",
                "job": "Casting",
                "profile_path": null
            },
            {
                "id": 1303,
                "name": "Alex McDowell",
                "department": "Art",
                "job": "Production Design",
                "profile_path": null
            },
            {
                "id": 7763,
                "name": "Ren Klyce",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7764,
                "name": "Richard Hymns",
                "department": "Sound",
                "job": "Sound Editor",
                "profile_path": null
            },
            {
                "id": 7467,
                "name": "David Fincher",
                "department": "Directing",
                "job": "Director",
                "profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
            },
            {
                "id": 7468,
                "name": "Chuck Palahniuk",
                "department": "Writing",
                "job": "Novel",
                "profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
            }
        ]
    }
})';
exit;

?>

javascript code:

<script language="javascript">
    jq(document).ready(function() {
     jq.ajax({
    url: 'test.php',
    dataType: "jsonp",
        });  });
        function getGenres(data){ 
            alert(data.genres.length);
        }  
    </script>

Your json response contains single quotes (') example 'bob' and you've which are not standard, so replace then by \' and then parse your json response.

For reference check jQuery single quote in JSON response

Remove ?( and ) from the starting and end of json response and also remove ' from them and check

var obj = jQuery.parseJSON( '{"adult":false,"backdrop_path":"/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":28,"name":"Action"},{"id":18,"name":"Drama"},{"id":53,"name":"Thriller"}],"homepage":"","id":550,"imdb_id":"tt0137523","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground fight clubs forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":"10.2188172784825","poster_path":"/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg","production_companies":[{"name":"20th Century Fox","id":25},{"name":"Fox 2000 Pictures","id":711},{"name":"Regency Enterprises","id":508}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-14","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"How much can you know about yourself if youve never been in a fight?","title":"Fight Club","vote_average":7.6,"vote_count":2787,"releases":{"countries":[{"iso_3166_1":"US","certification":"R","release_date":"1999-10-14"},{"iso_3166_1":"DE","certification":"18","release_date":"1999-11-10"},{"iso_3166_1":"GB","certification":"18","release_date":"1999-11-12"},{"iso_3166_1":"FR","certification":"16","release_date":"1999-11-10"},{"iso_3166_1":"TR","certification":"","release_date":"1999-12-10"},{"iso_3166_1":"BR","certification":"feibris","release_date":"1999-07-12"},{"iso_3166_1":"FI","certification":"K-18","release_date":"1999-11-12"},{"iso_3166_1":"BG","certification":"c","release_date":"2012-08-28"},{"iso_3166_1":"IT","certification":"VM14","release_date":"1999-10-29"}]},"trailers":{"quicktime":[],"youtube":[{"name":"Trailer 1","size":"HD","source":"SUXWAEX2jlg","type":"Trailer"}]},"credits":{"cast":[{"id":819,"name":"Edward Norton","character":"The Narrator","order":0,"cast_id":4,"profile_path":"/iUiePUAQKN4GY6jorH9m23cbVli.jpg"},{"id":287,"name":"Brad Pitt","character":"Tyler Durden","order":1,"cast_id":5,"profile_path":"/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"},{"id":1283,"name":"Helena Bonham Carter","character":"Marla Singer","order":2,"cast_id":6,"profile_path":"/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"},{"id":7470,"name":"Meat Loaf","character":"Robert Bob Paulson","order":3,"cast_id":7,"profile_path":"/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"},{"id":7499,"name":"Jared Leto","character":"Angel Face","order":4,"cast_id":30,"profile_path":"/msugySeTCyCmlRWtyB6sMixTQYY.jpg"},{"id":7471,"name":"Zach Grenier","character":"Richard Chesler","order":5,"cast_id":31,"profile_path":"/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"},{"id":7497,"name":"Holt McCallany","character":"The Mechanic","order":6,"cast_id":32,"profile_path":"/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"},{"id":7498,"name":"Eion Bailey","character":"Ricky","order":7,"cast_id":33,"profile_path":"/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"}],"crew":[{"id":7469,"name":"Jim Uhls","department":"Writing","job":"Author","profile_path":null},{"id":7474,"name":"Ross Grayson Bell","department":"Production","job":"Producer","profile_path":null},{"id":7475,"name":"Ceán Chaffin","department":"Production","job":"Producer","profile_path":null},{"id":1254,"name":"Art Linson","department":"Production","job":"Producer","profile_path":"/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"},{"id":7477,"name":"John King","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7478,"name":"Michael Simpson","department":"Sound","job":"Original Music Composer","profile_path":null},{"id":7479,"name":"Jeff Cronenweth","department":"Camera","job":"Director of Photography","profile_path":null},{"id":7480,"name":"James Haygood","department":"Editing","job":"Editor","profile_path":null},{"id":7481,"name":"Laray Mayfield","department":"Production","job":"Casting","profile_path":null},{"id":1303,"name":"Alex McDowell","department":"Art","job":"Production Design","profile_path":null},{"id":7763,"name":"Ren Klyce","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7764,"name":"Richard Hymns","department":"Sound","job":"Sound Editor","profile_path":null},{"id":7467,"name":"David Fincher","department":"Directing","job":"Director","profile_path":"/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"},{"id":7468,"name":"Chuck Palahniuk","department":"Writing","job":"Novel","profile_path":"/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"}]}}' );

alert( obj.genres.length );  

alert messages showing 3, so your json response is not valid

Community
  • 1
  • 1
Anubhav
  • 1,605
  • 4
  • 18
  • 31
  • 1
    Huh? The response is perfectly valid JSON (not that it technically matters since it's JSONP). But there's nothing wrong with single quotes within string values. No escaping is required. `"character": "Robert 'Bob' Paulson"` will work just fine as it is. – cookie monster Jan 25 '14 at 03:04
  • 1
    I think you didn't really read the question you linked to. The accepted answer says: *"Single quote characters do not need to be escaped"* – Felix Kling Jan 25 '14 at 03:10
  • 1
    *"Remove ?( and ) from the starting and end of json response"* It's actually a **JSONP** response. jQuery will replace `?` in the URL with a function name, so the response will eventually be `funcName(...)`. This will be included in a ` – Felix Kling Jan 25 '14 at 03:13
  • 1
    *"alert messages showing 3, so your json response is not valid"* If it wasn't valid JSON, then `jQuery.parseJSON` would return `null`, and you would not even be able to call `alert( obj.genres.length );`. As you can clearly see in the code in the question, `obj.genres` is an array of three elements, so of course the result will be `3`. What else would you expect? Or maybe I'm understanding this statement wrong. – Felix Kling Jan 25 '14 at 03:14
  • @FelixKling do not misunderstand my answer I have changed the json and checked it, and please read the full accepted answer in the given link. – Anubhav Jan 25 '14 at 03:22
  • @FelixKling is correct. You've not understood the other answer you linked to. I don't think you really understand JSON or JSONP. – cookie monster Jan 25 '14 at 03:34
  • @cookiemonster I am really sorry if I am posting something wrong, but it is not my ego I am just trying to help based on my opinion and what I have observed. After that I will delete my answer. – Anubhav Jan 25 '14 at 03:37
  • Sure, but if you are going to post, you need to be able to accept critiques of your answer. It isn't really a matter of opinions. It's simply that your answer isn't correct. If you need further explanation, ask. Or if you want to delete, that's fine too. – cookie monster Jan 25 '14 at 03:57
  • @cookiemonster I have made little change in jsonp response, ie in place of string "?" with callback function name and remove success: getGenres line from ajax then it will call getGeneres and everything works. Now tell me if it is feasible? – Anubhav Jan 25 '14 at 04:07
  • It will also work without those changes. jQuery takes care of creating the callback function. Sure, you can do it the way you did, but you don't have to and it's not advisable to do so. – Felix Kling Jan 25 '14 at 04:09
  • @FelixKling no, i tried it but it is not coming to getGenres function, that's why I changed the response – Anubhav Jan 25 '14 at 04:10
  • Are you actually calling the `https://api.themoviedb.org/3/movie/` API? Do you have an API key? – Felix Kling Jan 25 '14 at 04:13
  • @FelixKling No I am not calling the API but I am calling a dummy function which returns the same output mentioned in the question – Anubhav Jan 25 '14 at 04:17
  • @Anubhav: Well, that's the problem. jQuery will automatically substitute the `?` with a random function name and also create a function with such a name. Of course if you are using a function which returns the same response as above, it probably doesn't recognize the random function name in the `callback` parameter. Trust me, jQuery's JSONP implement works. There is nothing wrong with the OP's request or response, he just doesn't know how to access the data correctly. – Felix Kling Jan 25 '14 at 04:19
  • @FelixKling see updated answer, my implementation...check if I m doing wrong. – Anubhav Jan 25 '14 at 04:25
  • 1
    Please see http://stackoverflow.com/questions/6809053/simple-jquery-php-and-jsonp-example for an example of PHP + jQuery + JSONP. And don't use this question to learn JSONP. The changes you made to your answer are completely irrelevant to the OP's problem. And everything you considered as a problem is in fact not a problem. – Felix Kling Jan 25 '14 at 04:29