diff --git a/rfx.go b/rfx.go index 4e980f8..12d579a 100644 --- a/rfx.go +++ b/rfx.go @@ -885,7 +885,7 @@ func (r *rfx) StringMap() map[string]any { } // Slice 将当前值转换为 []any 切片 -func (r *rfx) Slice() []any { +func (r *rfx) Slice() (result []any) { v := r.value for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { if v.IsNil() { @@ -893,11 +893,20 @@ func (r *rfx) Slice() []any { } v = v.Elem() } - if !v.IsValid() || (v.Kind() != reflect.Slice && v.Kind() != reflect.Array) { + + if !v.IsValid() { + return nil + } else if v.Kind() == reflect.String { + if str := r.String(); len(str) >= 2 && str[0] == '[' { + if err := json.Unmarshal([]byte(str), &result); err == nil { + return result + } + } + } else if v.Kind() != reflect.Slice && v.Kind() != reflect.Array { return nil } - result := make([]any, v.Len()) + result = make([]any, v.Len()) for i := 0; i < v.Len(); i++ { elem := v.Index(i) for elem.Kind() == reflect.Ptr || elem.Kind() == reflect.Interface {