diff --git a/object.go b/object.go index c87be16..b726bdf 100644 --- a/object.go +++ b/object.go @@ -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