feat(Go-StudyExample):2020/12/28:新增【Go每日一库】sJson快速设置json串内容

This commit is contained in:
Huangzj
2020-12-28 17:42:48 +08:00
parent 19d04f5dcb
commit 3d04c6dbad
4 changed files with 129 additions and 0 deletions
+28
View File
@@ -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)
}
+94
View File
@@ -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)
}
+5
View File
@@ -0,0 +1,5 @@
获取包命令:go get github.com/tidwall/sjson
参考地址:https://segmentfault.com/a/1190000022148617
参考地址:https://github.com/darjun/go-daily-lib
+2
View File
@@ -37,6 +37,8 @@ json与struct之间转换处理工具使用示例
2020/12/28:新增【Go每日一库】air监听程序文件变化,主动重启工程
2020/12/28:新增【Go每日一库】sJson快速设置json串内容
# mod vendor模式加载包
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到