gitee.com/quant1x/pkg@v0.2.8/fastjson/handy_example_test.go (about) 1 package fastjson_test 2 3 import ( 4 "fmt" 5 "gitee.com/quant1x/pkg/fastjson" 6 ) 7 8 func ExampleGetString() { 9 data := []byte(`{"foo":{"bar":[123,"baz"]}}`) 10 11 s := fastjson.GetString(data, "foo", "bar", "1") 12 fmt.Printf("data.foo.bar[1] = %s", s) 13 14 // Output: 15 // data.foo.bar[1] = baz 16 } 17 18 func ExampleGetInt() { 19 data := []byte(`{"foo": [233,true, {"bar": [2343]} ]}`) 20 21 n1 := fastjson.GetInt(data, "foo", "0") 22 fmt.Printf("data.foo[0] = %d\n", n1) 23 24 n2 := fastjson.GetInt(data, "foo", "2", "bar", "0") 25 fmt.Printf("data.foo[2].bar[0] = %d\n", n2) 26 27 // Output: 28 // data.foo[0] = 233 29 // data.foo[2].bar[0] = 2343 30 } 31 32 func ExampleExists() { 33 data := []byte(`{"foo": [1.23,{"bar":33,"baz":null}]}`) 34 35 fmt.Printf("exists(data.foo) = %v\n", fastjson.Exists(data, "foo")) 36 fmt.Printf("exists(data.foo[0]) = %v\n", fastjson.Exists(data, "foo", "0")) 37 fmt.Printf("exists(data.foo[1].baz) = %v\n", fastjson.Exists(data, "foo", "1", "baz")) 38 fmt.Printf("exists(data.foobar) = %v\n", fastjson.Exists(data, "foobar")) 39 fmt.Printf("exists(data.foo.bar) = %v\n", fastjson.Exists(data, "foo", "bar")) 40 41 // Output: 42 // exists(data.foo) = true 43 // exists(data.foo[0]) = true 44 // exists(data.foo[1].baz) = true 45 // exists(data.foobar) = false 46 // exists(data.foo.bar) = false 47 }