失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python字符串转换为json_在python2.7中将原始字符串转换为JSON对象

python字符串转换为json_在python2.7中将原始字符串转换为JSON对象

时间:2019-08-09 07:44:11

相关推荐

python字符串转换为json_在python2.7中将原始字符串转换为JSON对象

我正在查询PostgreSQL服务器以获取数据,并且特定的json对象实际上以字符串形式返回.我试过跟随,但没有给出正确的输出:(这是i

python输出)

test

Out[103]: '{"max"=>28, "min"=>18, "custom"=>[{"id"=>"12345","name"=>"test_pur"}]}'

In[104]: test.replace("=>",":")

Out[104]: '{"max":28, "min":18, "custom":[{"id":"12345", "name":"test_pur"}]}'

In[105]: j_obj = json.dumps(test)

In[106]: j_obj

Out[106]: '"{\\"max\\"=>28, \\"min\\"=>18, \\"custom\\"=>[{\\"id\\"=>\\"12345\\", \\"name\\"=>\\"test_pur\\"}]}"'

如何通过识别“:”符号将字符串转换为json?

当我尝试“json.loads”时.以下是错误:

Traceback (most recent call last):

File "/Library/Python/2.7/site-packages/IPython/core/interactiveshell.py", line 3035, in run_code

exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in

data = json.loads(temp)

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 365, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode

obj, end = self.scan_once(s, idx)

ValueError: Expecting : delimiter: line 1 column 11 (char 10)

如果觉得《python字符串转换为json_在python2.7中将原始字符串转换为JSON对象》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。