在Python中将JSON转换为字符串
一开始我没有把我的问题解释清楚。 在python中将json转换为字符串时,请尝试使用在Python中将JSON转换为字符串,python,json,string,Python,Json,String,一开始我没有把我的问题解释清楚。 在python中将json转换为字符串时,请尝试使用str()和json.dumps() >>> data = {'jsonKey': 'jsonValue',"title": "hello world"} >>> print json.dumps(data) {"jsonKey": "jsonValue", "title":
str()
和json.dumps()
>>> data = {'jsonKey': 'jsonValue',"title": "hello world"}
>>> print json.dumps(data)
{"jsonKey": "jsonValue", "title": "hello world"}
>>> print str(data)
{'jsonKey': 'jsonValue', 'title': 'hello world'}
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world"}'
>>> str(data)
"{'jsonKey': 'jsonValue', 'title': 'hello world'}"
我的问题是:
>>> data = {'jsonKey': 'jsonValue',"title": "hello world'"}
>>> str(data)
'{\'jsonKey\': \'jsonValue\', \'title\': "hello world\'"}'
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world\'"}'
>>>
我的预期输出:“{'jsonKey':'jsonValue','title':'hello world'}”
>>data={'jsonKey':'jsonValue',“title”:“hello world”“}
文件“”,第1行
数据={'jsonKey':'jsonValue',“title:“hello world”“}
^
SyntaxError:扫描字符串文字时下线
>>>数据={'jsonKey':'jsonValue',“title:“hello world\”}
>>>json.dumps(数据)
“{”jsonKey:“jsonValue”,“title:“hello world\\\”}”
>>>str(数据)
“{'jsonKey\':\'jsonValue\',\'title\':\'hello world\'}”
我的预期输出:“{'jsonKey':'jsonValue','title':'hello world\'}”
我不需要再次将输出字符串更改为json(dict)
如何做到这一点?不仅仅是从Python对象中生成一个字符串,它始终会生成一个有效的字符串(假设对象中的所有内容都是可序列化的)
例如,如果其中一个值为None
,则str()
将生成无法加载的无效JSON:
>>> data = {'jsonKey': None}
>>> str(data)
"{'jsonKey': None}"
>>> json.loads(str(data))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 366, 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 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
还有其他区别。例如,
{'time':datetime.now()}
不能序列化为JSON,但可以转换为字符串。根据用途,您应该使用其中一种工具(即,结果稍后会被解码)。第二种形式本质上不是JSON。当您使用单引号和双引号时,会有很大的区别。使用str versionJSON加载。dumps()
用于转换为JSON,不是从JSON到字符串。str
与JSON完全无关;str(somedict)
看起来有点像JSON的事实是巧合str
获取对象的字符串表示形式,该对象可能与JSON完全不同(例如,对于实现\uuuu str\uuuu
的类)。@BAE JSON需要双引号字符串。JSON中的单引号字符串无效。事实上,我更感兴趣的是它们在输出字符串中的单引号和双引号的差异。@BAE好吧,在这种情况下很简单:。事实上,我更感兴趣的是它们在单引号和双引号中的差异。然后alecxe回答了您。
>>> data = {'jsonKey': None}
>>> str(data)
"{'jsonKey': None}"
>>> json.loads(str(data))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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 366, 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 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
>>> import json
>>> data = {'jsonKey': None}
>>> json.dumps(data)
'{"jsonKey": null}'
>>> json.loads(json.dumps(data))
{u'jsonKey': None}