alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[981] JSON module in Python

In Python, the json module provides functions for encoding and decoding JSON data. Here's a brief explanation of the commonly used functions:

  1. json.dump() and json.load() are functions for file processing.
  2. json.dumps() and json.loads() are functions for variable processing.
  3. dump(s): dict -> string
  4. load(s): string -> dict

  1. json.dumps(): This function is used to serialize Python objects to a JSON formatted string. It takes a Python object (such as a dictionary or a list) as input and returns a JSON formatted string.

    Example:

    import json
    data = {'name': 'John', 'age': 30, 'city': 'New York'}
    json_string = json.dumps(data)
    print(json_string)
  2. json.dump(): This function is similar to json.dumps(), but it writes the JSON data directly to a file-like object (such as a file object opened in write mode). It serializes the Python object to JSON and writes it to the specified file.

    Example:

    import json
    data = {'name': 'John', 'age': 30, 'city': 'New York'}
    with open('data.json', 'w') as json_file:
    json.dump(data, json_file)
  3. json.loads(): This function is used to deserialize a JSON formatted string into a Python object. It takes a JSON formatted string as input and returns a Python object (such as a dictionary or a list).

    Example:

    import json
    json_string = '{"name": "John", "age": 30, "city": "New York"}'
    data = json.loads(json_string)
    print(data)
  4. json.load(): This function is similar to json.loads(), but it reads JSON data from a file-like object (such as a file object opened in read mode) and deserializes it into a Python object.

    Example:

    import json
    with open('data.json', 'r') as json_file:
    data = json.load(json_file)
    print(data)

These functions are commonly used when working with JSON data in Python, allowing you to serialize Python objects to JSON format and vice versa.

posted on   McDelfino  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-02-26 【806】虾神-热点分析
2020-02-26 【461】word图片高清转成pdf
2013-02-26 【100】新学年的学习安排
点击右上角即可分享
微信分享提示