对从Zoho Creator返回的数据使用json.loads() - extra_data()错误

时间:2016-01-24 02:22:38

标签: python json python-requests zoho

我从zoho创作者api获得以下回复:

'var zohoview6 = {"Name_Registration":[     {"Approved":"Yes","Extension":1197,"ID_Number":"","Email":"xxx@yy.com","name_first  ":"Test","Updated":false,"ID":"1257609000000083070","Authorised":true,"NameID":68       }]};'

然后我使用str.replace删除' var zohoview6 ='

但是,当我使用json.loads(str)加载生成的str时,我收到以下错误

ValueError: Extra data: line 1 column 192 - line 1 column 193 (char 191 - 192)

这是我得到很好的格式化的回报:

var zohoview6 = 
{
    "Name_Registration":[
        {
        "Approved":"Yes",
        "Extension":1197,
        "ID_Number":"",
        "Email":"xxx@yy.com",
        "name_first":"Test",
        "Updated":false,
        "ID":"1257609000000083070",
        "Authorised":true,
        "NameID":68
        }]
};

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您还需要删除字符串末尾的额外;,例如str.rstrip()

>>> import json
>>>
>>> s = 'var zohoview6 = {"Name_Registration":[     {"Approved":"Yes","Extension":1197,"ID_Number":"","Email":"xxx@yy.com","name_first  ":"Test","Updated":false,"ID":"1257609000000083070","Authorised":true,"NameID":68       }]};'
>>> data = s.replace("var zohoview6 = ", "").rstrip(";")
>>> json.loads(data)
{u'Name_Registration': [{u'Updated': False, u'Extension': 1197, u'ID_Number': u'', u'Email': u'xxx@yy.com', u'Authorised': True, u'name_first  ': u'Test', u'ID': u'1257609000000083070', u'NameID': 68, u'Approved': u'Yes'}]}