Home  >  Article  >  Backend Development  >  How to format json

How to format json

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2024-01-22 09:34:093455browse

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.

How to format json

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn