2021-03-11 00:05:56 +08:00
|
|
|
// Package json...
|
|
|
|
//
|
|
|
|
// Description : json...
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-11 00:05:56 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-10 11:44 下午
|
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-03-14 23:31:08 +08:00
|
|
|
// TestJSON ...
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-14 23:31:08 +08:00
|
|
|
//
|
|
|
|
// Date : 10:58 下午 2021/3/14
|
2021-03-11 00:05:56 +08:00
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
tree := NewDynamicJSON()
|
2021-03-11 22:12:14 +08:00
|
|
|
fmt.Println(tree.extraSliceIndex("[200]"))
|
2021-04-13 22:11:52 +08:00
|
|
|
tree.SetValue("extra.height.value", 180, false)
|
|
|
|
tree.SetValue("extra.height.unit.use", "cm", false)
|
|
|
|
tree.SetValue("extra.height.unit.open", "1", false)
|
|
|
|
tree.SetValue("name", "zhangdeman", false)
|
|
|
|
tree.SetValue("good.name", "good", false)
|
|
|
|
tree.SetValue("work", "111", false)
|
|
|
|
tree.SetValue("good.price", "180", false)
|
|
|
|
tree.SetValue("good.unit", "$", false)
|
|
|
|
tree.SetValue("slice.[0].name", "zhang", false)
|
|
|
|
tree.SetValue("slice.[1].name", "de", false)
|
|
|
|
tree.SetValue("slice.[2].name", "man", false)
|
|
|
|
tree.SetValue("slice.[3]", "zhangdeman", false)
|
2021-03-11 17:02:44 +08:00
|
|
|
fmt.Println(tree.String())
|
2021-03-14 23:36:06 +08:00
|
|
|
tree = NewDynamicJSON()
|
2021-04-13 22:11:52 +08:00
|
|
|
tree.SetValue("[0]", "zhang", false)
|
|
|
|
tree.SetValue("[1]", "de", false)
|
|
|
|
tree.SetValue("[2]", "man", false)
|
2021-03-14 23:36:06 +08:00
|
|
|
fmt.Println(tree.String())
|
2021-03-11 00:05:56 +08:00
|
|
|
}
|
2021-03-14 23:31:08 +08:00
|
|
|
|
|
|
|
// TestType 判断数据类型断言
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-14 23:31:08 +08:00
|
|
|
//
|
|
|
|
// Date : 10:59 下午 2021/3/14
|
|
|
|
func TestType(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
2021-04-13 21:53:36 +08:00
|
|
|
|
|
|
|
// TestSelect 测试动态选择字段
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-13 21:53:36 +08:00
|
|
|
//
|
|
|
|
// Date : 9:47 下午 2021/4/13
|
|
|
|
func TestSelect(t *testing.T) {
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"name": "zhangdeman",
|
|
|
|
"extra": map[string]interface{}{
|
|
|
|
"age": 18,
|
|
|
|
"height": 180,
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
},
|
|
|
|
"slice": []int{1, 2, 3},
|
2021-04-13 22:14:32 +08:00
|
|
|
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
|
2021-04-13 23:43:04 +08:00
|
|
|
"table": []map[string]interface{}{
|
|
|
|
{"name": "alex", "age": 18, "number": 1},
|
|
|
|
{"name": "bob", "age": 28, "number": 2},
|
|
|
|
},
|
2021-04-13 21:53:36 +08:00
|
|
|
}
|
2021-04-13 23:43:04 +08:00
|
|
|
pathList := []string{"name", "extra.age", "slice", "map", "table.[].name|number|test"}
|
2021-04-13 21:53:36 +08:00
|
|
|
r, e := NewParseJSONTree(source).Parse(pathList)
|
|
|
|
fmt.Println(r.String(), e)
|
|
|
|
}
|