Na początku nie wyjaśniłem dokładnie moich pytań. Spróbuj użyć funkcji str() i json.dumps() podczas konwertowania json na ciąg znaków w pythonie.Konwersja json na ciąg w pytonie
>>> 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'}"
Moje pytanie brzmi:
>>> data = {'jsonKey': 'jsonValue',"title": "hello world'"}
>>> str(data)
'{\'jsonKey\': \'jsonValue\', \'title\': "hello world\'"}'
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world\'"}'
>>>
Moje oczekiwany wynik: "{ 'jsonKey': 'jsonValue', 'title': 'Hello World' '}"
>>>
>>> data = {'jsonKey': 'jsonValue',"title": "hello world""}
File "<stdin>", line 1
data = {'jsonKey': 'jsonValue',"title": "hello world""}
^
SyntaxError: EOL while scanning string literal
>>> data = {'jsonKey': 'jsonValue',"title": "hello world\""}
>>> json.dumps(data)
'{"jsonKey": "jsonValue", "title": "hello world\\""}'
>>> str(data)
'{\'jsonKey\': \'jsonValue\', \'title\': \'hello world"\'}'
Moje oczekiwane wyniki: "{'jsonKey': 'jsonValue', 'title': 'hello world \"'} "
PS: Zmiana ciągów wyjściowych na json (dict) aga nie jest konieczna dla mnie.
Jak to zrobić? Dzięki.
Druga forma nie jest zasadniczo JSON. – Tibo
Istnieje duża różnica, gdy masz pojedynczy vs cudzysłowach, wkładaj z json.loads użyciu wersji Str –
'json.dumps()' jest do konwersji ** ** do JSON, a nie z JSON do łańcucha. – Barmar