将 jsonString 转换为 Python 中可解析的东西?
Posted
技术标签:
【中文标题】将 jsonString 转换为 Python 中可解析的东西?【英文标题】:Convert jsonString to something parseable in Python? 【发布时间】:2015-04-03 08:06:36 【问题描述】:我无法在 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)
我做错了什么?
谢谢大家!
编辑:在 cmets 中描述的尝试方法:
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"]
这看起来不错不是吗?为什么不解析?
【问题讨论】:
为什么不使用result.json()
啊哈。所以不能从一开始就解析 result.text 吗?
【参考方案1】:
无论出于何种原因,服务器都会向您发送一个表示字符串的 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']
【讨论】:
以上是关于将 jsonString 转换为 Python 中可解析的东西?的主要内容,如果未能解决你的问题,请参考以下文章
将 JsonString 转换为自定义的 java ArrayList
将 JsonString 转换为自定义的 java ArrayList