python json加载错误(期望属性名称)

时间:2016-02-18 16:22:39

标签: python json

我正在尝试从json结果返回特定字段。以下是我要用它做的事情:

rjson = json.loads("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")

但是,运行时出现以下错误:

ValueError: Expecting property name: line 1 column 2 (char 1)

编辑:

这是完整的程序(减去API密钥):

import requests, json
r = requests.get('https://api.themoviedb.org/3/find/'+'tt1022603'+'?api_key=###&external_source=imdb_id')
print str(r.json())
rjson = json.loads("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")

我在哪里做load()我只是从上面的行中打印出来。

3 个答案:

答案 0 :(得分:1)

这是您尝试加载的不是有效的JSON 。它看起来更像是字典的字符串表示。如果您无法将输入数据更改为有效的JSON,请通过ast.literal_eval()加载它,这将安全地评估字符串

books.author_id

答案 1 :(得分:1)

那是因为你的字符串不是有效的JSON,而是python字典作为字符串。你想要的是ast.literal_eval

>>> from ast import literal_eval
>>> literal_eval("{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'genre_ids': [35, 18, 10749], u'id': 19913, u'original_language': u'en'}]}")
{u'tv_season_results': [], u'tv_episode_results': [], u'person_results': [], u'tv_results': [], u'movie_results': [{u'poster_path': u'/5SjtNPD1bb182vzQccvEUpXHFjN.jpg', u'title': u'(500) Days of Summer', u'overview': u'Tom (Joseph Gordon-Levitt), greeting-card writer and hopeless romantic, is caught completely off-guard when his girlfriend, Summer (Zooey Deschanel), suddenly dumps him. He reflects on their 500 days together to try to figure out where their love affair went sour, and in doing so, Tom rediscovers his true passions in life.', u'release_date': u'2009-07-17', u'popularity': 3.598717, u'original_title': u'(500) Days of Summer', u'id': 19913, u'vote_count': 1239, u'video': False, u'adult': False, u'vote_average': 7.3, u'original_language': u'en', u'backdrop_path': u'/yYw9cVdRJ4zzwxM2cTDXfT6JI6E.jpg', u'genre_ids': [35, 18, 10749]}]}

答案 2 :(得分:0)

响应的json()方法为您解析JSON并返回一个JSON对象。您可以直接访问它,而无需再次尝试解析它。

r = requests.get('https://api.themoviedb.org/3/find/'+'tt1022603'+'?api_key=###&external_source=imdb_id')
data = r.json()
print data['tv_season_results']