JSON (JavaScript Object Notation) 格式化可以使用缩进来实现。可以使用许多编程语言中的库或工具来达到这个目的,例如在 Python 中可以使用 json.dumps() 函数并将 indent 参数设置为所需的缩进量。在 JavaScript 中,可以使用 JSON.stringify() 函数并将第二个参数设置为一个函数来达到相同的目的。
举个例子:
Python
import json
data = {'name': 'John Smith', 'age': 35, 'city': 'New York'}
json_data = json.dumps(data, indent=4)
print(json_data)
JavaScript
const data = {'name': 'John Smith', 'age': 35, 'city': 'New York'};
const json_data = JSON.stringify(data, null, 4);
console.log(json_data);
output:
{
"name": "John Smith",
"age": 35,
"city": "New York"
}
在上面的示例中,输出的 JSON 数据被缩进了 4 个空格。