You can use the json module in Python to format JSON data, use the json.loads() function to parse the original JSON data, and use the json.dumps() function to format the parsed data with indentation. The string can be.
#In Python, you can use the json module to format JSON data. Here is an example:
import json # 原始的JSON数据 json_data = '{"name": "John", "age": 30, "city": "New York"}' # 解析JSON数据 parsed_data = json.loads(json_data) # 格式化输出 formatted_data = json.dumps(parsed_data, indent=4) print(formatted_data)
This will output the formatted JSON data:
{ "name": "John", "age": 30, "city": "New York" }
In the above example, use the json.loads() function to parse the raw JSON data and use The json.dumps() function formats the parsed data into an indented string. By setting the value of the indent parameter to 4, you can specify the number of spaces for indentation.
The above is the detailed content of How to format json. For more information, please follow other related articles on the PHP Chinese website!