import json
print(json.dumps(eval(r.content), indent=4, ensure_ascii=False))

json格式化输出

import json
import sys

true = True
false = False

for line in sys.stdin:
    jsonString = line.strip()
    print json.dumps(eval(jsonString), indent=4, ensure_ascii=False)

读取文件并转字典

在Python中,你可以使用 json模块来读取JSON格式的文件,并将其转换为字典。以下是一个简单的步骤说明和代码示例:

  1. 确保你有一个JSON文件。例如,假设你有一个名为 data.json的文件,其内容如下:
{
  "name": "John",
  "age": 30,
  "city": "New York"
}
  1. 使用 json模块的 load函数来读取文件并将其转换为字典:
import json

# 打开JSON文件
with open('data.json', 'r') as file:
    # 读取JSON文件并将其转换为字典
    data_dict = json.load(file)

# 打印字典以验证内容
print(data_dict)

当你运行这段代码时,data_dict将会是一个字典,其内容为:

{'name': 'John', 'age': 30, 'city': 'New York'}

这样,你就成功地将JSON文件读取并转换为了Python字典。