From e5cb42bd74e83b3a0a1ef835a97bf833ad3d3c4f Mon Sep 17 00:00:00 2001 From: John Date: Thu, 25 Jan 2018 10:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3gparser=E5=AF=B9Go=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=A4=84=E7=90=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/encoding/gjson/gjson.go | 17 ++++++++++++++--- geg/encoding/gparser.go | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/g/encoding/gjson/gjson.go b/g/encoding/gjson/gjson.go index ced5df549..2ebc95a0c 100644 --- a/g/encoding/gjson/gjson.go +++ b/g/encoding/gjson/gjson.go @@ -99,8 +99,18 @@ func LoadContent (data []byte, t string) (*Json, error) { } // 将变量转换为Json对象进行处理,该变量至少应当是一个map或者array,否者转换没有意义 -func NewJson(v interface{}) *Json { - return &Json{ p: &v } +func NewJson(value interface{}) *Json { + switch value.(type) { + case map[string]interface{}: + return &Json{ p: &value } + case []interface{}: + return &Json{ p: &value } + default: + // 这里效率会比较低 + b, _ := Encode(value) + v, _ := Decode(b) + return &Json{ p: &v } + } } // 将指定的json内容转换为指定结构返回,查找失败或者转换失败,目标对象转换为nil @@ -272,7 +282,8 @@ func (j *Json) convertValue(value interface{}) interface{} { case []interface{}: return value default: - // 这里效率会比较低 + // 这里效率会比较低,当然比直接用反射也不会差到哪儿去 + // 为了操作的灵活性,牺牲了一定的效率 b, _ := Encode(value) v, _ := Decode(b) return v diff --git a/geg/encoding/gparser.go b/geg/encoding/gparser.go index 859b677f1..5ad8afaa8 100644 --- a/geg/encoding/gparser.go +++ b/geg/encoding/gparser.go @@ -171,5 +171,5 @@ func convert() { } func main() { - multiDots2() + convert() } \ No newline at end of file