无法将JSON转换为Python列表

时间:2016-04-07 13:55:06

标签: python json google-analytics

我正在为Google Analytics创建一个信息中心,我希望将来自Google Analytics的JSON转换为python列表,但不断收到此错误:

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

以下是应该转换它的代码:

results = get_results(service, profile)
data = json.loads(str(results))
return data

结果等于:

{u'columnHeaders': [{u'columnType': u'METRIC',
                 u'dataType': u'INTEGER',
                 u'name': u'ga:sessions'}],
u'containsSampledData': False,
u'id': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxxxxx&metrics=ga:sessions&start-date=7daysAgo&end-date=today',
u'itemsPerPage': 1000,
u'kind': u'analytics#gaData',
u'profileInfo': {u'accountId': u'xxxxxxxx',
              u'internalWebPropertyId': u'xxxxxxxx',
              u'profileId': u'xxxxxxxx',
              u'profileName': u'xxxxxxxx',
              u'tableId': u'ga:xxxxxxxx',
              u'webPropertyId': u'UA-xxxxxxxx-1'},
u'query': {u'end-date': u'today',
        u'ids': u'ga:xxxxxxxx',
        u'max-results': 1000,
        u'metrics': [u'ga:sessions'],
        u'start-date': u'7daysAgo',
        u'start-index': 1},
u'rows': [[u'109826']],
u'selfLink': u'https://www.googleapis.com/analytics/v3/data/ga?ids=ga:76270671&metrics=ga:sessions&start-date=7daysAgo&end-date=today',
u'totalResults': 1,
u'totalsForAllResults': {u'ga:sessions': u'109826'}}

我已删除了上面的一些帐户详细信息,但希望能够了解相关信息。

谢谢,

亚伦

1 个答案:

答案 0 :(得分:2)

你的结果看起来不像字符串更像是python对象(dict) (因为每个字符串前面都有unicode标记) 也许你应该试试这个:

print(type(result))