擅长:python、mysql、java
<p>当您试图在一个名为<code>TEST.json</code>的文件中写入此内容时,我将假定此字符串是较大json字符串的一部分。在</p>
<p>让我举一个完整的例子:</p>
<pre><code>js = '''{"a": "and voila!\\u00c2\\u00a0At the moment you can't use our"}'''
print(js)
{"a": "and voila!\u00c2\u00a0At the moment you can't use our"}
</code></pre>
<p>我首先用json加载它:</p>
^{pr2}$
<p>好了,这看起来像是一个utf-8字符串,它被错误地解码为Latin1。让我们反向操作:</p>
<pre><code>x['a'] = x['a'].encode('latin1').decode('utf8')
print(x)
print(x['a'])
{'a': "and voila!\xa0At the moment you can't use our"}
and voila! At the moment you can't use our
</code></pre>
<p>好了,现在可以把它转换回正确的json字符串:</p>
<pre><code>print(json.dumps(x))
{"a": "and voila!\\u00a0At the moment you can\'t use our"}
</code></pre>
<p>表示正确编码的不间断空格(U+00A0)</p>
<p>你应该做的是:</p>
<pre><code># load the string as json:
js = json.loads(request)
# identify the string values in the json - you probably know how but I don't...
...
# convert the strings:
js[...] = js[...].encode('latin1').decode('utf8')
# convert back to a json string
request = json.dumps(js)
</code></pre>