gitee.com/quant1x/gox@v1.7.6/fastjson/handy_example_test.go (about)

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