refactor(object): 调整JSONBaseObject处理顺序并添加遍历方法

- 调整了JSONObject.Put方法中类型判断的顺序,将JSONBaseObject类型的
  处理移到最后,避免与*JSONObject和*JSONArray的判断冲突

- 新增Entries()方法用于获取所有键值对映射
- 新增Values()方法用于获取所有值的切片
```
This commit is contained in:
程广 2026-02-11 17:51:27 +08:00
parent d8c339a39a
commit 7b7115fecd
1 changed files with 22 additions and 3 deletions

View File

@ -54,13 +54,14 @@ func (jo *JSONObject) Get(key string) (interface{}, bool) {
// Put 设置指定键的值如果是JSONBaseObject则提取其值
func (jo *JSONObject) Put(key string, value interface{}) {
switch v := value.(type) {
case JSONBaseObject:
// 如果是JSONBaseObject转换为实际值
jo.data[key] = extractValueFromJSONBaseObject(v)
case *JSONObject:
jo.data[key] = v.data
case *JSONArray:
jo.data[key] = v.data
case JSONBaseObject:
// 如果是JSONBaseObject转换为实际值
jo.data[key] = extractValueFromJSONBaseObject(v)
default:
jo.data[key] = v
}
@ -96,6 +97,24 @@ func (jo *JSONObject) ContainsKey(key string) bool {
return exists
}
// Entries 获取所有键值对
func (jo *JSONObject) Entries() map[string]interface{} {
result := make(map[string]interface{})
for k, v := range jo.data {
result[k] = v
}
return result
}
// Values 获取所有值
func (jo *JSONObject) Values() []interface{} {
values := make([]interface{}, 0, len(jo.data))
for _, v := range jo.data {
values = append(values, v)
}
return values
}
// GetData 获取内部数据
func (jo *JSONObject) GetData() map[string]interface{} {
return jo.data