json.loads(contents) 显示中文
时间: 2023-09-14 20:09:44 浏览: 131
你可以在调用 json.loads() 函数时,传入参数 ensure_ascii=False,这样就可以保留中文了。例如:
```python
import json
data = {'name': '张三', 'age': 18}
json_str = json.dumps(data, ensure_ascii=False)
print(json_str)
```
输出结果为:
```json
{"name": "张三", "age": 18}
```
相关问题
python2环境 json.loads(contents) 显示中文
在 Python2 中,`json.loads()` 方法默认会将中文字符串转换为 Unicode 编码。如果您想要将其显示为中文,可以将其转换为 UTF-8 编码,然后使用 `decode()` 方法转换为 Unicode 编码。示例代码如下:
```python
import json
# 假设 contents 是包含中文的 JSON 字符串
contents = '{"name": "张三", "age": 18}'
# 将 JSON 字符串转换为 Python 对象
data = json.loads(contents)
# 将 Python 对象转换为 JSON 字符串,并指定 ensure_ascii 参数为 False
json_str = json.dumps(data, ensure_ascii=False)
# 将 JSON 字符串转换为 UTF-8 编码,并使用 decode() 方法转换为 Unicode 编码
unicode_str = json_str.encode('utf-8').decode('unicode_escape')
# 打印结果
print(unicode_str)
```
输出结果为:
```
{"name": "张三", "age": 18}
```
with open("api_working_temp.json", "r") as f: print(f.read()) api_working_dict = json.load(f)
As an AI language model, I don't have access to the file "api_working_temp.json" as it is not provided in the code snippet. However, I can explain what this code snippet does.
This code reads the contents of a file named "api_working_temp.json" using the "with open" statement. It then prints the contents of the file using the "print" function. Finally, it loads the contents of the file into a Python dictionary using the "json.load" function and assigns it to the variable "api_working_dict".
阅读全文