From 3d04c6dbad992572d0f847ee687cb95aff79cfbd Mon Sep 17 00:00:00 2001 From: Huangzj Date: Mon, 28 Dec 2020 17:42:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(Go-StudyExample):2020/12/28=EF=BC=9A?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=90Go=E6=AF=8F=E6=97=A5=E4=B8=80?= =?UTF-8?q?=E5=BA=93=E3=80=91sJson=E5=BF=AB=E9=80=9F=E8=AE=BE=E7=BD=AEjson?= =?UTF-8?q?=E4=B8=B2=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/sJson/AdvanceSJson_test.go | 28 +++++++++ example/sJson/BasicSJson_test.go | 94 ++++++++++++++++++++++++++++++ example/sJson/readme.md | 5 ++ readme.md | 2 + 4 files changed, 129 insertions(+) create mode 100644 example/sJson/AdvanceSJson_test.go create mode 100644 example/sJson/BasicSJson_test.go create mode 100644 example/sJson/readme.md diff --git a/example/sJson/AdvanceSJson_test.go b/example/sJson/AdvanceSJson_test.go new file mode 100644 index 0000000..1889797 --- /dev/null +++ b/example/sJson/AdvanceSJson_test.go @@ -0,0 +1,28 @@ +/* + * @Author : huangzj + * @Time : 2020/12/28 17:32 + * @Description: + */ + +package sJson + +import ( + "fmt" + "github.com/tidwall/sjson" + "testing" +) + +func TestAdvanceSJson(t *testing.T) { + //通过通配符的方式进行匹配找到对应字段,这个是没办法做到的所以这边会报错 + user := `{"name":"dj","age":18}` + newValue, err := sjson.Set(user, "na?e", "dajun") + fmt.Println(err, newValue) + +} + +func TestErrorJson(t *testing.T) { + //SJson不会检查json串是否正确,只会把设置的值进行返回,所以在使用的时候需要进行json串的正确性校验 + user := `{"name":dj,age:18}` + newValue, err := sjson.Set(user, "name", "dajun") + fmt.Println(err, newValue) +} diff --git a/example/sJson/BasicSJson_test.go b/example/sJson/BasicSJson_test.go new file mode 100644 index 0000000..1c793dd --- /dev/null +++ b/example/sJson/BasicSJson_test.go @@ -0,0 +1,94 @@ +/* + * @Author : huangzj + * @Time : 2020/12/28 17:29 + * @Description: + */ + +package sJson + +import ( + "fmt" + "github.com/tidwall/sjson" + "testing" +) + +func TestSJsonSet(t *testing.T) { + //测试设值 + const json = `{"name":{"first":"li","last":"dj"},"age":18}` + + value, _ := sjson.Set(json, "name.last", "dajun") + fmt.Println(value) + +} + +func TestSJsonDelete(t *testing.T) { + //进行字段删除 + var newValue string + user := `{"name":{"first":"li","last":"dj"},"age":18}` + + newValue, _ = sjson.Delete(user, "name.first") + fmt.Println(newValue) + + newValue, _ = sjson.Delete(user, "name.full") + fmt.Println(newValue) + + fruits := `{"fruits":["apple", "orange", "banana"]}` + + newValue, _ = sjson.Delete(fruits, "fruits.1") + fmt.Println(newValue) + + newValue, _ = sjson.Delete(fruits, "fruits.-1") + fmt.Println(newValue) + + newValue, _ = sjson.Delete(fruits, "fruits.5") + fmt.Println(newValue) +} + +//测试操作数组,【.下标】表示替代的是哪个位置的元素,-1比较特殊是在数组后面进行添加,如果超过数组长度会自动填充 null上去,这个好像有点尴尬 +func TestSJsonOperateList(t *testing.T) { + fruits := `{"fruits":["apple", "orange", "banana"]}` + + var newValue string + newValue, _ = sjson.Set(fruits, "fruits.1", "grape") + fmt.Println(newValue) + + newValue, _ = sjson.Set(fruits, "fruits.3", "pear") + fmt.Println(newValue) + + newValue, _ = sjson.Set(fruits, "fruits.-1", "strawberry") + fmt.Println(newValue) + + newValue, _ = sjson.Set(fruits, "fruits.5", "watermelon") + fmt.Println(newValue) + +} + +type User struct { + Name string `json:"name"` + Age int `json:"age"` +} + +//测试SJson支持设置的所有类型 +func TestSJsonAllType(t *testing.T) { + nilJSON, _ := sjson.Set("", "key", nil) + fmt.Println(nilJSON) + + boolJSON, _ := sjson.Set("", "key", false) + fmt.Println(boolJSON) + + intJSON, _ := sjson.Set("", "key", 1) + fmt.Println(intJSON) + + floatJSON, _ := sjson.Set("", "key", 10.5) + fmt.Println(floatJSON) + + strJSON, _ := sjson.Set("", "key", "hello") + fmt.Println(strJSON) + + mapJSON, _ := sjson.Set("", "key", map[string]interface{}{"hello": "world"}) + fmt.Println(mapJSON) + + u := User{Name: "dj", Age: 18} + structJSON, _ := sjson.Set("", "key", u) + fmt.Println(structJSON) +} diff --git a/example/sJson/readme.md b/example/sJson/readme.md new file mode 100644 index 0000000..a2d9a32 --- /dev/null +++ b/example/sJson/readme.md @@ -0,0 +1,5 @@ +获取包命令:go get github.com/tidwall/sjson + +参考地址:https://segmentfault.com/a/1190000022148617 + +参考地址:https://github.com/darjun/go-daily-lib \ No newline at end of file diff --git a/readme.md b/readme.md index 1ed4668..26296ae 100644 --- a/readme.md +++ b/readme.md @@ -37,6 +37,8 @@ json与struct之间转换处理工具使用示例 2020/12/28:新增【Go每日一库】air监听程序文件变化,主动重启工程 +2020/12/28:新增【Go每日一库】sJson快速设置json串内容 + # mod vendor模式加载包 通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到