2014-01-25 1 views
0

Я пытаюсь разобрать JSON данные, как это:Синтаксический JSONP с Javascript

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' 

выше URL с ключом апи включают возвращает этот результат:

?({ 
    "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" 
      } 
     ] 
    } 
}) 

Я использую это, чтобы разобрать его однако я не везет

$(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(', '); 

Пль ase Помощь

+0

у вас есть сообщение об ошибке? и этот первый знак вопроса появляется в фактическом возвращенном jsonp. –

+0

* «однако мне не повезло» * означает, что именно? Не могли бы вы предоставить более подробное описание проблемы, иначе вам действительно сложно помочь. Является ли 'entries.genres.name' фактически массивом? Я думаю, проблема заключается не столько в том, что вы не можете * разобрать * ответ, но что вы не знаете, как получить доступ к свойствам результирующего объекта. –

+0

@PatrickEvans, к сожалению, нет, я не получил сообщение об ошибке, которое могло бы указать мне в правильном направлении. Все было скопировано, поскольку оно было сохранено. – Craig

ответ

1

entries.genres - это массив. Он не имеет собственности .name. Вы должны получить сообщение об ошибке в консоли разработчика вашего браузера для доступа к .length из 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" 
     } 
    ], 
... 
} 

Так что вам нужно перебирать entries.genres, затем нажмите .name текущего жанра, если это то, что вы хотите.

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


На другой ноте, у вас есть два неявных глобалам.

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

Забыв запятую после var entries = data, следующие две строки будут неявно создавать глобальные переменные, так как они не являются частью var заявления.

Вот почему я всегда использую передовые запятые для объявлений переменных. Делает это очевидным, когда отсутствует запятая.

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

Я пробовал ваши предложения, однако никаких результатов. может быть, я пропустил что-то, что вы можете проверить? http://jsfiddle.net/Eh276/1/ – Craig

+1

@Craig: Вам действительно нужно научиться использовать консоль: 'Uncaught ReferenceError: данные не определены'. –

+0

@Craig: вам нужно выполнить части кода, которые я исправил, и вставить их в исходный код. Вы не можете просто взять фрагменты кода и ожидать их запуска. –

-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 код:

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

Ваш ответ JSON содержит одиночные кавычки (') пример 'боб' и вы имеете, которые не являются стандартными, так замените затем на \ ", а затем проанализируйте свой ответ json.

Для контрольной проверки jQuery single quote in JSON response

Удалить? (И) от запуска и окончания реакции JSon, а также удалить "из них и проверить

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

предупредительных сообщений, показывающих 3, так что ваш ответ JSON является недействительно

+1

А? Ответ вполне применим JSON (а не то, что это технически имеет значение, поскольку это JSONP). Но нет ничего плохого в одинарных кавычках в строковых значениях. Не требуется экранирование. «персонаж»: «Роберт Боб« Полсон »« будет работать так же хорошо, как есть. –

+0

@cookiemonster Я отредактировал свой ответ – Anubhav

+1

Я думаю, вы действительно не читали вопрос, с которым вы связались. В принятом ответе говорится: * «Одиночные кавычки не нужно экранировать» * –

 Смежные вопросы

  • Нет связанных вопросов^_^