添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

python json压缩

在 Python 中,可以使用 json 模块来处理 JSON 数据。json 模块提供了两个函数:json.dumps() 和 json.dump(),可以将 Python 对象转换为 JSON 字符串。

import json
data = {
    'name': 'John',
    'age': 30,
    'city': 'New York'
json_data = json.dumps(data)
print(json_data)
{"name": "John", "age": 30, "city": "New York"}

如果你想压缩 JSON 字符串,可以使用 gzip 模块来压缩字符串。例如:

import gzip
import json
data = {
    'name': 'John',
    'age': 30,
    'city': 'New York'
json_data = json.dumps(data)
compressed_data = gzip.compress(json_data.encode())
print(compressed_data)

这将输出压缩后的 JSON 字符串。

注意:压缩后的数据是二进制数据,如果你想将它转换回字符串,可以使用 gzip.decompress() 函数。

decompressed_data = gzip.decompress(compressed_data).decode()
print(decompressed_data)

这将输出原始的 JSON 字符串。

  •