JSON 工具 - JSONUtil

由来

JSONUtil 基于 Python 内置 json 模块封装增强,提供更便捷的 JSON 操作。

方法

序列化

from hutool import JSONUtil

# 转 JSON 字符串
JSONUtil.to_json_str({"name": "test", "value": 123})
# '{"name": "test", "value": 123}'

# 格式化输出
JSONUtil.to_json_pretty_str({"name": "test"})
# {
#     "name": "test"
# }

解析

# 解析 JSON 字符串
obj = JSONUtil.parse_obj('{"name": "test"}')     # {"name": "test"}
arr = JSONUtil.parse_array('[1, 2, 3]')           # [1, 2, 3]
data = JSONUtil.parse('{"key": "value"}')          # 自动判断类型

Bean 转换

class User:
    def __init__(self):
        self.name = ""
        self.age = 0

# JSON 转对象
user = JSONUtil.to_bean('{"name": "张三", "age": 25}', User)
print(user.name)  # "张三"

# JSON 数组转对象列表
users = JSONUtil.to_bean_list('[{"name": "张三"}, {"name": "李四"}]', User)

# 对象转 JSON
JSONUtil.from_bean(user)  # '{"name": "张三", "age": 25}'

文件操作

# 读取 JSON 文件
data = JSONUtil.read_json("config.json")
obj = JSONUtil.read_json_object("config.json")
arr = JSONUtil.read_json_array("data.json")

# 写入 JSON 文件
JSONUtil.write_json("output.json", {"key": "value"}, indent=2)

路径操作

data = {"user": {"profile": {"name": "张三", "age": 25}}}

# 按路径获取值
JSONUtil.get_by_path(data, "user.profile.name")  # "张三"

# 按路径设置值
JSONUtil.put_by_path(data, "user.profile.email", "test@example.com")

判断

JSONUtil.is_json('{"key": "value"}')     # True
JSONUtil.is_json_obj('{"key": "value"}') # True
JSONUtil.is_json_array('[1, 2, 3]')      # True
JSONUtil.is_json("not json")             # False
JSONUtil.is_json(None)                   # False(None/空字符串返回 False)
JSONUtil.is_json("")                     # False

格式化

# 格式化 JSON
JSONUtil.format_json('{"a":1,"b":2}')
# {
#   "a": 1,
#   "b": 2
# }

# 压缩 JSON(去除空白)
JSONUtil.compress('{\n  "a": 1,\n  "b": 2\n}')
# '{"a":1,"b":2}'

键名映射

# 递归映射字典键名
data = {"user_name": "test", "user_info": {"home_address": "NYC"}}
JSONUtil.map_dict_keys(data, lambda k: k.upper())
# {"USER_NAME": "test", "USER_INFO": {"HOME_ADDRESS": "NYC"}}

# 映射列表中每个字典的键名
items = [{"item_name": "a"}, {"item_name": "b"}]
JSONUtil.map_list_keys(items, lambda k: k.upper())

# snake_case → camelCase
JSONUtil.convert_keys_to_camel({"user_name": "test"})
# {"userName": "test"}

# camelCase → snake_case
JSONUtil.convert_keys_to_snake({"userName": "test"})
# {"user_name": "test"}

JSON 比较

# jsonEqual — 比较两个 JSON 是否相等
JSONUtil.json_equal('{"a": 1, "b": 2}', '{"b": 2, "a": 1}')  # True

# jsonKeysEqual — 比较两个 JSON 的键是否相同
JSONUtil.json_keys_equal('{"a": 1, "b": 2}', '{"a": 3, "b": 4}')  # True

有序 JSON

# getOrderedJson — 获取按键排序的 JSON 字符串
JSONUtil.get_ordered_json({"b": 2, "a": 1})  # '{"a": 1, "b": 2}'

XML 转 JSON

# parseFromXml — 从 XML 解析为 JSON
data = JSONUtil.parse_from_xml('<root><name>test</name><value>123</value></root>')
# {"name": "test", "value": "123"}