17.06.2015, 09:50
|
Новичок на форуме
|
|
Регистрация: 10.06.2015
Сообщений: 7
|
|
Преобразование в объект
Доброго времени суток. В общем следующая проблема. С помощью следующей библиотеки получаю с сайта список фильмов с описанием. Список приходит вот в таком виде (Ответ по запросу Fight Club)
{"page":1,"results":[{"adult":false,"backdrop_path":"/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg","genre_ids":[18],"id":550,"original_language":"en","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.","release_date":"1999-10-14","poster_path":"/811DjJTon9gD6hZ8nCjSitaIXFQ.jpg","popularity":4.987446,"title":"Fight Club","video":false,"vote_average":7.8,"vote_count":3523},{"adult":false,"backdrop_path":"/qrZssI8koUdRxkYnrOKMRY3m5Fq.jpg","genre_ids":[27],"id":289732,"original_language":"zh","original_title":"Zombie Fight Club","overview":"It's the end of the century at a corner of the city in a building riddled with crime - Everyone in the building has turned into zombies. After Jenny's boyfriend is killed in a zombie attack, she faces the challenge of surviving in the face of adversity. In order to stay alive, she struggles with Andy to flee danger.","release_date":"2014-10-23","poster_path":"/7k9db7pJyTaVbz3G4eshGltivR1.jpg","popularity":1.793271,"title":"Zombie Fight Club","video":false,"vote_average":3.5,"vote_count":2},{"adult":false,"backdrop_path":"/uywB4ZS6iCuc4aCIsAOyhW4sFZJ.jpg","genre_ids":[18,28,53,80],"id":14476,"original_language":"en","original_title":"Clubbed","overview":"An underworld drama set in the early 1980s, about a lonely factory worker whose life is transformed when he becomes a nightclub doorman.","release_date":"2009-01-16","poster_path":"/mGR71N18bh26uwtAYZuiKOc79m9.jpg","popularity":1.602033,"title":"Clubbed","video":false,"vote_average":7.1,"vote_count":15},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":259016,"original_language":"en","original_title":"Insane Fight Club","overview":"A group of friends have created a brand new subculture that is taking over the streets of Glasgow. They've established their very own fight club, but this is no ordinary wrestling event - this is brutal, riotous chaos. Fights don't always stay inside the ring, people are bounced off the side of buses and thrown off balconies in pubs. They now plan the biggest show of their lives. The stakes are high, will it bring them the fame and recognition they need to survive?","release_date":"2014-03-11","poster_path":"/mLhwBQPV3iATe3L61kbpmxANwL8.jpg","popularity":1.011834,"title":"Insane Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/5sWZRCsjzH1Dxo6gSBeX4RpAQ4p.jpg","genre_ids":[28,18,80],"id":51021,"original_language":"en","original_title":"Lure: Teen Fight Club","overview":"A community is under siege as three Belmont Highschool coed students go missing with no trace of their whereabouts. The pressure is on the police to capture the culprits responsible. Scouring the school hallways in search of clues, undercover female detective Maggie Rawdon (Jessica Sonnerborn) enters Belmont High as a transfer student in an attempt to solve the hideous disappearance of the students. Maggie makes a few new friends, and gets invited to a private rave in the country. Just as the group begins to suspect that they've taken a wrong turn, however, the trap is sprung and Maggie finds out firsthand what fate has befallen the missing girls.","release_date":"2010-11-16","poster_path":"/aRTX5Y52yGbVL6TGnyI4E8jjtz4.jpg","popularity":1.005904,"title":"Lure: Teen Fight Club","video":false,"vote_average":2.0,"vote_count":1},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":151912,"original_language":"de","original_title":"Jurassic Fight Club","overview":"Jurassic Fight Club, a paleontology-based miniseries that ran for 12 episodes, depicts how prehistoric beasts hunted their prey, dissecting these battles and uncovering a predatory world far more calculated and complex than originally thought. It was hosted by George Blasing, a self-taught paleontologist.","release_date":"2008-10-22","poster_path":"/AwECEjjen4eYSDZ3AETXnFG6dgu.jpg","popularity":1.001073,"title":"Jurassic Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/tcoAGvTo96R7Y9ZGVCCz7BZvrvb.jpg","genre_ids":[28,53],"id":104782,"original_language":"en","original_title":"Florence Fight Club","overview":"Four men decided to enter in the oldest Fight Club of the History, The Florentine Football tournament. A father and son, a black guy, an old champion and outsider clerk will enter in an arena of the time to win their fears, to go over their limits, to be heroes for a day.","release_date":"2010-01-01","poster_path":"/eQqqu0srTYcclWqylvgpLyU87hV.jpg","popularity":1.001,"title":"Florence Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":289100,"original_language":"en","original_title":"Girls Fight Club","overview":"The best women's wrestling competition of all time...and if you think it's fake you're in for a big surprise See LEGENDARY Mixed Martial Arts fighters coach their teams to victory in the cage! aka Chuck Lidell's Girl's Fight Club","release_date":"2009-06-18","poster_path":null,"popularity":1.000857,"title":"Girls Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":295477,"original_language":"da","original_title":"Fight club camp kusse","overview":null,"release_date":"2005-08-12","poster_path":"/5itVi2OcKAkTUK0xtVxueKURb1W.jpg","popularity":1.000429,"title":"Fight club camp kusse","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":209599,"original_language":"en","original_title":"Brooklyn Girls Fight Club","overview":"From the birthplace of boxing legend Mike Tyson, young women brawl in secret fight clubs to win $1000 and invaluable street cred.","release_date":null,"poster_path":"/luWpP5WSw9JjbWS1J4BMnjkkJCX.jpg","popularity":1.0,"title":"Brooklyn Girls Fight Club","video":false,"vote_average":3.5,"vote_count":1},{"adult":false,"backdrop_path":"/rsv46SYFz7SxWVbC48uV26Xu1x9.jpg","genre_ids":[28,18,80],"id":219897,"original_language":"de","original_title":"Barrio Brawler","overview":"A martial arts instructor must enter the world of illegal pit fighting in order to save his family and his dojo.","release_date":"2013-08-27","poster_path":"/8AyCFlw1d856UAAN21fnWOpQu4l.jpg","popularity":1.000123,"title":"Barrio Brawler","video":false,"vote_average":1.5,"vote_count":2},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":322772,"original_language":"en","original_title":"Insane Fight Club II - This Time It’s Personal","overview":"Insane Fight Club is back. This year the boys are taking their unique form of entertainment to England as they stage fight nights in Birmingham, Leeds, Liverpool and Newcastle.","release_date":"2015-01-21","poster_path":"/84mcV9ky4TSpD7dpHnsx7yBVs64.jpg","popularity":1.016131,"title":"Insane Fight Club II - This Time It’s Personal","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":115584,"original_language":"en","original_title":"Fight Club – The “I am Jack’s Laryngitis” Edit","overview":"Edward Norton’s voiceover has been completely removed, making this a much more introspective and “arty” film. I believe it puts you even MORE inside the head of the main character. It’s kind of hard to explain, really. You’ll have to check it out.\r The narration is completely in the center channel of the 5.1 mix, so I simply faded out any instance of it. I had to use the soundtrack to fill in a few musical holes, had to do a bit of foley work (new audio for the Ron Popeil infomercial on TV, whistling as Jack is leaving his job with the shopping cart), and had to trim a second or two off a couple of shots (very minimal and unintrusive).","release_date":null,"poster_path":null,"popularity":1.000017,"title":"Fight Club – The “I am Jack’s Laryngitis” Edit","video":false,"vote_average":0.0,"vote_count":0}],"total_pages":1,"total_results":13}
по виду это обычный JS объект. Но проблема в том, что если добавить этот результат в переменную а потом запросить тип переменной, то ответ будут string, JS думает, что это строка. Если принудительно преобразовать её в объект, то получится совсем ересь (выкладывать сюда не буду, чтобы не создавать портянку).
Отсюда вопрос, как преобразовать полученные с сервера данные в JS объект и продолжить с ними работать.
П.С. Если полученный результат присвоить переменной сохранить в файл и работать с этим файлом, то все отлично работает.
П.С.С Если надо будет , могу выложить демо страницу, где можно на реальном примере пощупать
Последний раз редактировалось Alex_D181, 17.06.2015 в 09:53.
|
|
17.06.2015, 09:55
|
Профессор
|
|
Регистрация: 12.12.2012
Сообщений: 1,398
|
|
Alex_D181,
var some = {"page":1,"results":[{"adult":false,"backdrop_path":"/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg","genre_ids":[18],"id":550,"original_language":"en","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.","release_date":"1999-10-14","poster_path":"/811DjJTon9gD6hZ8nCjSitaIXFQ.jpg","popularity":4.987446,"title":"Fight Club","video":false,"vote_average":7.8,"vote_count":3523},{"adult":false,"backdrop_path":"/qrZssI8koUdRxkYnrOKMRY3m5Fq.jpg","genre_ids":[27],"id":289732,"original_language":"zh","original_title":"Zombie Fight Club","overview":"It's the end of the century at a corner of the city in a building riddled with crime - Everyone in the building has turned into zombies. After Jenny's boyfriend is killed in a zombie attack, she faces the challenge of surviving in the face of adversity. In order to stay alive, she struggles with Andy to flee danger.","release_date":"2014-10-23","poster_path":"/7k9db7pJyTaVbz3G4eshGltivR1.jpg","popularity":1.793271,"title":"Zombie Fight Club","video":false,"vote_average":3.5,"vote_count":2},{"adult":false,"backdrop_path":"/uywB4ZS6iCuc4aCIsAOyhW4sFZJ.jpg","genre_ids":[18,28,53,80],"id":14476,"original_language":"en","original_title":"Clubbed","overview":"An underworld drama set in the early 1980s, about a lonely factory worker whose life is transformed when he becomes a nightclub doorman.","release_date":"2009-01-16","poster_path":"/mGR71N18bh26uwtAYZuiKOc79m9.jpg","popularity":1.602033,"title":"Clubbed","video":false,"vote_average":7.1,"vote_count":15},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":259016,"original_language":"en","original_title":"Insane Fight Club","overview":"A group of friends have created a brand new subculture that is taking over the streets of Glasgow. They've established their very own fight club, but this is no ordinary wrestling event - this is brutal, riotous chaos. Fights don't always stay inside the ring, people are bounced off the side of buses and thrown off balconies in pubs. They now plan the biggest show of their lives. The stakes are high, will it bring them the fame and recognition they need to survive?","release_date":"2014-03-11","poster_path":"/mLhwBQPV3iATe3L61kbpmxANwL8.jpg","popularity":1.011834,"title":"Insane Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/5sWZRCsjzH1Dxo6gSBeX4RpAQ4p.jpg","genre_ids":[28,18,80],"id":51021,"original_language":"en","original_title":"Lure: Teen Fight Club","overview":"A community is under siege as three Belmont Highschool coed students go missing with no trace of their whereabouts. The pressure is on the police to capture the culprits responsible. Scouring the school hallways in search of clues, undercover female detective Maggie Rawdon (Jessica Sonnerborn) enters Belmont High as a transfer student in an attempt to solve the hideous disappearance of the students. Maggie makes a few new friends, and gets invited to a private rave in the country. Just as the group begins to suspect that they've taken a wrong turn, however, the trap is sprung and Maggie finds out firsthand what fate has befallen the missing girls.","release_date":"2010-11-16","poster_path":"/aRTX5Y52yGbVL6TGnyI4E8jjtz4.jpg","popularity":1.005904,"title":"Lure: Teen Fight Club","video":false,"vote_average":2.0,"vote_count":1},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":151912,"original_language":"de","original_title":"Jurassic Fight Club","overview":"Jurassic Fight Club, a paleontology-based miniseries that ran for 12 episodes, depicts how prehistoric beasts hunted their prey, dissecting these battles and uncovering a predatory world far more calculated and complex than originally thought. It was hosted by George Blasing, a self-taught paleontologist.","release_date":"2008-10-22","poster_path":"/AwECEjjen4eYSDZ3AETXnFG6dgu.jpg","popularity":1.001073,"title":"Jurassic Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":"/tcoAGvTo96R7Y9ZGVCCz7BZvrvb.jpg","genre_ids":[28,53],"id":104782,"original_language":"en","original_title":"Florence Fight Club","overview":"Four men decided to enter in the oldest Fight Club of the History, The Florentine Football tournament. A father and son, a black guy, an old champion and outsider clerk will enter in an arena of the time to win their fears, to go over their limits, to be heroes for a day.","release_date":"2010-01-01","poster_path":"/eQqqu0srTYcclWqylvgpLyU87hV.jpg","popularity":1.001,"title":"Florence Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":289100,"original_language":"en","original_title":"Girls Fight Club","overview":"The best women's wrestling competition of all time...and if you think it's fake you're in for a big surprise See LEGENDARY Mixed Martial Arts fighters coach their teams to victory in the cage! aka Chuck Lidell's Girl's Fight Club","release_date":"2009-06-18","poster_path":null,"popularity":1.000857,"title":"Girls Fight Club","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":295477,"original_language":"da","original_title":"Fight club camp kusse","overview":null,"release_date":"2005-08-12","poster_path":"/5itVi2OcKAkTUK0xtVxueKURb1W.jpg","popularity":1.000429,"title":"Fight club camp kusse","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":209599,"original_language":"en","original_title":"Brooklyn Girls Fight Club","overview":"From the birthplace of boxing legend Mike Tyson, young women brawl in secret fight clubs to win $1000 and invaluable street cred.","release_date":null,"poster_path":"/luWpP5WSw9JjbWS1J4BMnjkkJCX.jpg","popularity":1.0,"title":"Brooklyn Girls Fight Club","video":false,"vote_average":3.5,"vote_count":1},{"adult":false,"backdrop_path":"/rsv46SYFz7SxWVbC48uV26Xu1x9.jpg","genre_ids":[28,18,80],"id":219897,"original_language":"de","original_title":"Barrio Brawler","overview":"A martial arts instructor must enter the world of illegal pit fighting in order to save his family and his dojo.","release_date":"2013-08-27","poster_path":"/8AyCFlw1d856UAAN21fnWOpQu4l.jpg","popularity":1.000123,"title":"Barrio Brawler","video":false,"vote_average":1.5,"vote_count":2},{"adult":false,"backdrop_path":null,"genre_ids":[99],"id":322772,"original_language":"en","original_title":"Insane Fight Club II - This Time It’s Personal","overview":"Insane Fight Club is back. This year the boys are taking their unique form of entertainment to England as they stage fight nights in Birmingham, Leeds, Liverpool and Newcastle.","release_date":"2015-01-21","poster_path":"/84mcV9ky4TSpD7dpHnsx7yBVs64.jpg","popularity":1.016131,"title":"Insane Fight Club II - This Time It’s Personal","video":false,"vote_average":0.0,"vote_count":0},{"adult":false,"backdrop_path":null,"genre_ids":[],"id":115584,"original_language":"en","original_title":"Fight Club – The “I am Jack’s Laryngitis” Edit","overview":"Edward Norton’s voiceover has been completely removed, making this a much more introspective and “arty” film. I believe it puts you even MORE inside the head of the main character. It’s kind of hard to explain, really. You’ll have to check it out.\r The narration is completely in the center channel of the 5.1 mix, so I simply faded out any instance of it. I had to use the soundtrack to fill in a few musical holes, had to do a bit of foley work (new audio for the Ron Popeil infomercial on TV, whistling as Jack is leaving his job with the shopping cart), and had to trim a second or two off a couple of shots (very minimal and unintrusive).","release_date":null,"poster_path":null,"popularity":1.000017,"title":"Fight Club – The “I am Jack’s Laryngitis” Edit","video":false,"vote_average":0.0,"vote_count":0}],"total_pages":1,"total_results":13};
console.log(some);
Запустите и смотрите в консоль, вполне нормальный объект, проблема в месте получения (наверно вы просто получаете JSON строку)
Может это поможет?
var some = JSON.parse('{"some": 123}');
alert(some.some + " " + typeof some);
Последний раз редактировалось tsigel, 17.06.2015 в 09:58.
|
|
17.06.2015, 09:58
|
Новичок на форуме
|
|
Регистрация: 10.06.2015
Сообщений: 7
|
|
Всем спасибо, нагуглил ответ, парсить надо через JSON.parse('{"page": 1}')
|
|
|
|