1. 程式人生 > 其它 >go語言gabs庫的使用

go語言gabs庫的使用

 1 import "fmt"
 2 import "github.com/Jeffail/gabs"
 3 
 4 //1、Search + path 使用方法
 5 func main() {
 6     jObj, _ := gabs.ParseJSON([]byte(
 7         `
 8 {
 9     "info": {
10         "name": {
11             "first": "lee",
12             "last": "darjun"
13         },
14         "age": 18,
15         "
hobbies": [ 16 "game", 17 "programming" 18 ] 19 } 20 } 21 `)) 22 //如果傳入的路徑有誤或路徑下無資料,則Search/Path方法返回的gabs.Container物件內部資料為nil,即Data()方法返回nil 23 fmt.Println("first name: ", jObj.Search("info", "name", "first").Data()) 24 fmt.Println("second name: ", jObj.Path("
info.name.last").Data()) 25 fmt.Println("one hobby: ", jObj.Path("info.hobbies").Index(1).Data()) 26 }
 1 import "fmt"
 2 import "github.com/Jeffail/gabs"
 3 
 4 //2、S——遞迴查詢
 5 func main() {
 6     jObj, _ := gabs.ParseJSON([]byte(
 7         `
 8     {
 9         "user": {
10             "name": "
dj", 11 "age": 18, 12 "members": [{ 13 "name": "hjw", 14 "age": 20, 15 "relation": "spouse" 16 }, 17 { 18 "name": "lizi", 19 "age": 3, 20 "relation": "son" 21 } 22 ] 23 } 24 } 25 `)) 26 27 fmt.Println("member names: ", jObj.S("user", "members", "name").Data()) 28 fmt.Println("member ages: ", jObj.S("user", "members", "age").Data()) 29 fmt.Println("member relations: ", jObj.S("user", "members", "relation").Data()) 30 fmt.Println("spouse name: ", jObj.S("user", "members").Index(0).S("name").Data()) 31 }
 1 import "fmt"
 2 import "github.com/Jeffail/gabs"
 3 
 4 //3、
 5 //3.1:Children():返回所有陣列元素的切片,如果在物件上呼叫該方法,Children()將以不確定順序返回物件所有的值的切片;
 6 //3.2:ChildrenMap():返回物件的鍵和值。
 7 func main() {
 8     jObj, _ := gabs.ParseJSON([]byte(
 9         `
10     {
11         "user": {
12             "name": "dj",
13             "age": 18,
14             "members": [{
15                     "name": "hjw",
16                     "age": 20,
17                     "relation": "spouse"
18                 },
19                 {
20                     "name": "lizi",
21                     "age": 3,
22                     "relation": "son"
23                 }
24             ]
25         }
26     }
27 `))
28 
29     KeyValue,_ := jObj.S("user").ChildrenMap()
30     for k, v := range KeyValue {
31         fmt.Printf("key: %v, value: %v\n", k, v)
32     }
33 
34     fmt.Println()
35 
36     Value,_ := jObj.S("user", "members").Children()
37     for i, v := range Value {
38         fmt.Printf("member %d: %v\n", i+1, v)
39     }
40 }
 1 //4
 2 //4.1:Exists(hierarchy ...string);
 3 //4.2:ExistsP(path string):方法名以P結尾,表示接受以.分隔的路徑。
 4 func main() {
 5     jObj, _ := gabs.ParseJSON([]byte(
 6         `
 7     {
 8         "user": {
 9             "name": "dj",
10             "age": 18
11         }
12     }
13 `))
14     fmt.Printf("has name? %t\n", jObj.Exists("user", "name"))
15     fmt.Printf("has age? %t\n", jObj.ExistsP("user.age"))
16     fmt.Printf("has job? %t\n", jObj.Exists("user", "job"))
17 }
 1 //5、
 2 //5.1:獲取陣列大小:ArrayCount/ArrayCountP,不加字尾的方法接受可變引數作為路徑,以P為字尾的方法需要傳入.分隔的路徑;
 3 //5.2:獲取陣列某個索引的元素:ArrayElement/ArrayElementP。
 4 func main() {
 5     jObj, _ := gabs.ParseJSON([]byte(
 6         `
 7     {
 8         "user": {
 9             "name": "dj",
10             "age": 18,
11             "members": [{
12                     "name": "hjw",
13                     "age": 20,
14                     "relation": "spouse"
15                 },
16                 {
17                     "name": "lizi",
18                     "age": 3,
19                     "relation": "son"
20                 }
21             ],
22             "hobbies": ["game", "programming"]
23         }
24     }
25 `))
26 
27     cnt, _ := jObj.ArrayCount("user", "members")
28     fmt.Println("member count:", cnt)
29     cnt, _ = jObj.ArrayCount("user", "hobbies")
30     fmt.Println("hobby count:", cnt)
31 
32     ele, _ := jObj.ArrayElement(0, "user", "members")
33     fmt.Println("first member:", ele)
34     ele, _ = jObj.ArrayElement(1, "user", "hobbies")
35     fmt.Println("second hobby:", ele)
36 }
 1 //6、
 2 //6.1:Set(value interface{}, hierarchy ...string):將路徑各個部分作為可變引數傳入即可;
 3 //6.2:SetP(value interface{}, path string):路徑各個部分以點.分隔;
 4 func main() {
 5     gObj := gabs.New()
 6 
 7     gObj.Set("lee", "info", "name", "first")
 8     gObj.SetP("darjun", "info.name.last")
 9     gObj.SetP(18, "info.age")
10 
11     fmt.Println(gObj.StringIndent("", "  "))
12 }
 1 //7、
 2 //7.1:通過Array/ArrayP分別在路徑user.hobbies和user.bugs下建立陣列,
 3 //7.2:然後呼叫ArrayAppend/ArrayAppendP向這兩個陣列中新增元素
 4 func main() {
 5     gObj := gabs.New()
 6 
 7     arrObj1, _ := gObj.Array("user", "hobbies")
 8     fmt.Println(arrObj1.String())
 9 
10     arrObj2, _ := gObj.ArrayP("user.bugs")
11     fmt.Println(arrObj2.String())
12 
13     gObj.ArrayAppend("game", "user", "hobbies")
14     gObj.ArrayAppend("programming", "user", "hobbies")
15 
16     gObj.ArrayAppendP("crash", "user.bugs")
17     gObj.ArrayAppendP("panic", "user.bugs")
18     fmt.Println(gObj.StringIndent("", "  "))
19 }
 1 //8、物件刪除使用Delete/DeleteP這組方法
 2 func main() {
 3     jObj, _ := gabs.ParseJSON([]byte(
 4         `
 5     {
 6         "info": {
 7             "age": 18,
 8             "name": {
 9                 "first": "lee",
10                 "last": "darjun"
11             }
12         }
13     }
14 `))
15     fmt.Println(jObj.StringIndent("", "  "))
16 
17     jObj.Delete("info", "name")
18     fmt.Println(jObj.StringIndent("", "  "))
19 
20     jObj.Delete("info")
21     fmt.Println(jObj.StringIndent("", "  "))
22 }
 1 //9、刪除某個索引的陣列元素,使用ArrayRemove/ArrayRemoveP方法
 2 func main() {
 3     jObj, _ := gabs.ParseJSON([]byte(
 4         `
 5     {
 6         "user": {
 7             "bugs": ["crash", "panic"],
 8             "hobbies": ["game", "programming"]
 9         }
10     }
11 `))
12 
13     jObj.ArrayRemove(0, "user", "bugs")
14     fmt.Println(jObj.StringIndent("", "  "))
15     fmt.Println()
16     jObj.ArrayRemoveP(1, "user.hobbies")
17     fmt.Println(jObj.StringIndent("", "  "))
18 }
 1 //10、我們可以將兩個gabs.Container合併成一個。如果同一個路徑下有相同的鍵:
 2     //10.1、如果兩者都是物件型別,則對二者進行合併操作;
 3     //10.2、如果兩者都是陣列型別,則將後者中所有元素追加到前一個數組中;
 4     //10.3、其中一個為陣列,合併之後另一個同名鍵的值將會作為元素新增到陣列中。
 5 func main() {
 6     obj1, _ := gabs.ParseJSON([]byte(`{"user":{"name":"dj"}}`))
 7     obj2, _ := gabs.ParseJSON([]byte(`{"user":{"age":18}}`))
 8     obj1.Merge(obj2)
 9     fmt.Println(obj1.StringIndent("", "  "))
10 
11     arr1, _ := gabs.ParseJSON([]byte(`{"user": {"hobbies": ["game"]}}`))
12     arr2, _ := gabs.ParseJSON([]byte(`{"user": {"hobbies": ["programming"]}}`))
13     arr1.Merge(arr2)
14     fmt.Println(arr1.StringIndent("", "  "))
15 
16     obj3, _ := gabs.ParseJSON([]byte(`{"user":{"name":"dj", "hobbies": "game"}}`))
17     arr3, _ := gabs.ParseJSON([]byte(`{"user": {"hobbies": ["programming"]}}`))
18     obj3.Merge(arr3)
19     fmt.Println(obj3.StringIndent("", "  "))
20 
21     obj4, _ := gabs.ParseJSON([]byte(`{"user":{"name":"dj", "hobbies": "game"}}`))
22     arr4, _ := gabs.ParseJSON([]byte(`{"user": {"hobbies": ["programming"]}}`))
23     arr4.Merge(obj4)
24     fmt.Println(arr4.StringIndent("", "  "))
25 
26     obj5, _ := gabs.ParseJSON([]byte(`{"user":{"name":"dj", "hobbies": {"first": "game"}}}`))
27     arr5, _ := gabs.ParseJSON([]byte(`{"user": {"hobbies": ["programming"]}}`))
28     obj5.Merge(arr5)
29     fmt.Println(obj5.StringIndent("", "  "))
30 }
Mamba never out