我正在查询PostgreSQL服务器以获取数据,实际上返回了一个特定的json对象作为字符串。我试着跟随,但没有给出正确的输出:(这是ipython输出)
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 "<ipython-input-12-a7562191decf>", line 1, in <module>
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)发布于 2015-07-21 16:23:47
尝试使用json.loads()将JSON字符串加载(反序列化)到Python dict中
>>> import json
>>> s = '{"max":28, "min":18, "custom":[{"id":"12345", "name":"test_pur"}]}'
>>> data = json.loads(s)
>>> print data["max"]
28发布于 2015-07-21 16:23:39
你的代码是错误的。您应该使用json.loads()而不是json.dumps() (如果我正确理解了您的问题)。在这里,您要将原始字符串转换为JSON字符串。我想你想把JSON字符串转换成JSON对象,不是吗?
https://stackoverflow.com/questions/31533693
复制相似问题