gpt4 book ai didi

python - 将 jsonString 转换为可在 Python 中解析的内容?

转载 作者:太空宇宙 更新时间:2023-11-04 03:37:26 25 4
gpt4 key购买 nike

我无法在 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"}]}

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

最佳答案

无论出于何种原因,服务器都会向您发送一个表示字符串的 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'}]}

关于python - 将 jsonString 转换为可在 Python 中解析的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28310586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com