将json String转换为Python中可解析的内容?

时间:2015-02-03 23:13:06

标签: python json

我在python中将jsonString转换为可解析的json时遇到了问题。我的脚本从网络服务器获取jsonData,如下所示:

results = requests.post(url, headers=headers, data=data)
print results

>>"{\"result\":[{\"0\":\"005d000000450RiAAI\",\"1\":\"003J00000160bqeIAA\",\"2\":\"a09J000000CmRjYIAV\",\"3\":\"Rob Robenson\",\"4\":\"Zac Rosenberg\",\"5\":\"Class IV - Lead\"}, {\"0\":\"005d000000450RiAAI\",\"1\":\"003J0000016ZjuCIAS\",\"2\":\"a09J000000CxvgCIAR\",\"3\":\"Bob Bobenson\",\"4\":\"Zac Rosenberg\",\"5\":\"Class III - Quote/Deal\"}]}"

我使用json.loads来解析,得到:

json_results = json.loads(results.text)
print json_results

>>{"result":[{
               "0":"005d000000450RiAAI",
               "1":"003J00000160bqeIAA",
               "2":"a09J000000CmRjYIAV",
               "3":"Rob Robenson",
               "4":"Zac Rosenberg",
               "5":"Class IV - Lead"
              },
              {
               "0":"005d000000450RiAAI",
               "1":"003J0000016ZjuCIAS",
               "2":"a09J000000CxvgCIAR",
               "3":"Bob Bobenson",
               "4":"Zac Rosenberg",
               "5":"Class III - Quote/Deal"
             }]
  }

有趣的是,当我得到json_results的类型时:

print type(json_results)
>>unicode

那应该是那种类型吗?

当我尝试解析json时,我得到:

print json_results['result']
>>TypeError: string indices must be integers

我试过替换json字符串中的'\':

results = requests.post(url, headers=headers, data=data)
results = results.text.replace('\\','')
json_results = json.loads(results)

>>File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
json/decoder.py", line 368, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 4 - line 1 column 299 (char 3 - 298)

我做错了什么?

谢谢大家!

编辑:评论中描述的尝试方法:

json_results = results.json()
print json_results['result']

>>TypeError: string indices must be integers

然而,

json_results = results.json()
print json_results

>>{"result":[{"0":"005d000000450RiAAI","1":"003J00000160bqeIAA","2":"a09J000000CmRjYIAV","3":"Rob Robenson","4":"Zac Rosenberg","5":"Class IV - Lead"}, {"0":"005d000000450RiAAI","1":"003J0000016ZjuCIAS","2":"a09J000000CxvgCIAR","3":"Bob Bobenson","4":"Zac Rosenberg","5":"Class III - Quote/Deal"}]}

这看起来不错不是吗?为什么不解析?

1 个答案:

答案 0 :(得分:2)

无论出于何种原因,服务器都会向您发送一个表示字符串的JSON文档,该字符串本身就是一个表示字典的JSON文档 - 因此您必须对其进行两次解码:

json_results = json.loads(results.json())

您可以通过pprint了解更清楚的内容:

>>> from pprint import pprint

>>> pprint(results.text)
'"{\\"result\\":[{\\"0\\":\\"005d000000450RiAAI\\",\\"1\\":\\"003J00000160bqeIAA\\",\\"2\\":\\"a09J000000CmRjYIAV\\",\\"3\\":\\"Rob Robenson\\",\\"4\\":\\"Zac Rosenberg\\",\\"5\\":\\"Class IV - Lead\\"}, {\\"0\\":\\"005d000000450RiAAI\\",\\"1\\":\\"003J0000016ZjuCIAS\\",\\"2\\":\\"a09J000000CxvgCIAR\\",\\"3\\":\\"Bob Bobenson\\",\\"4\\":\\"Zac Rosenberg\\",\\"5\\":\\"Class III - Quote/Deal\\"}]}"'

>>> pprint(json.loads(results.text))
u'{"result":[{"0":"005d000000450RiAAI","1":"003J00000160bqeIAA","2":"a09J000000CmRjYIAV","3":"Rob Robenson","4":"Zac Rosenberg","5":"Class IV - Lead"}, {"0":"005d000000450RiAAI","1":"003J0000016ZjuCIAS","2":"a09J000000CxvgCIAR","3":"Bob Bobenson","4":"Zac Rosenberg","5":"Class III - Quote/Deal"}]}'

>>> pprint(json.loads(json.loads(results.text)))
{u'result': [{u'0': u'005d000000450RiAAI',
            u'1': u'003J00000160bqeIAA',
            u'2': u'a09J000000CmRjYIAV',
            u'3': u'Rob Robenson',
            u'4': u'Zac Rosenberg',
            u'5': u'Class IV - Lead'},
            {u'0': u'005d000000450RiAAI',
            u'1': u'003J0000016ZjuCIAS',
            u'2': u'a09J000000CxvgCIAR',
            u'3': u'Bob Bobenson',
            u'4': u'Zac Rosenberg',
            u'5': u'Class III - Quote/Deal'}]}