@@ -17,53 +17,38 @@ It is inspired by [jmoiron/jsonq](https://github.com/jmoiron/jsonq). Feel free t
17
17
package main
18
18
19
19
import (
20
- " fmt"
21
- " github.com/xiaost/jsonport"
20
+ " fmt"
21
+
22
+ " github.com/xiaost/jsonport"
22
23
)
23
24
24
25
func main () {
25
26
jsonstr := ` {
26
27
"timestamp": "1438194274",
27
28
"users": [{"id": 1, "name": "Tom"}, {"id": 2, "name": "Peter"}],
28
- "keywords": ["golang", "json"]
29
+ "keywords": ["golang", "json"],
30
+ "status": 1
29
31
}`
30
- j , _ := Unmarshal ([]byte (jsonstr))
31
-
32
- // Output: Tom
33
- fmt.Println (j.GetString (" users" , 0 , " name" ))
32
+ j , _ := jsonport.Unmarshal ([]byte (jsonstr))
34
33
35
- // Output: [golang json]
36
- fmt.Println (j.Get (" keywords" ).StringArray ())
37
-
38
- // Output: [Tom Peter]
39
- names , _ := j.Get (" users" ).EachOf (" name" ).StringArray ()
40
- fmt.Println (names)
34
+ fmt.Println (j.GetString (" users" , 0 , " name" )) // Tom, nil
35
+ fmt.Println (j.Get (" keywords" ).StringArray ()) // [golang json], nil
36
+ fmt.Println (j.Get (" users" ).EachOf (" name" ).StringArray ()) // [Tom Peter], nil
41
37
42
38
// try parse STRING as NUMBER
39
+ fmt.Println (j.Get (" timestamp" ).Int ()) // 0, type mismatch: expected NUMBER, found STRING
43
40
j.StringAsNumber ()
44
- // Output: 1438194274
45
- fmt.Println (j.Get (" timestamp" ).Int ())
41
+ fmt.Println (j.Get (" timestamp" ).Int ()) // 1438194274, nil
46
42
47
43
// convert NUMBER, STRING, ARRAY and OBJECT type to BOOL
44
+ fmt.Println (j.GetBool (" status" )) // false, type mismatch: expected BOOL, found NUMBER
48
45
j.AllAsBool ()
49
- // Output: false
50
- fmt.Println (j.GetBool (" status" ))
46
+ fmt.Println (j.GetBool (" status" )) // true, nil
51
47
52
48
// using Unmarshal with path which can speed up json decode
53
- j, _ = Unmarshal ([]byte (jsonstr), " users" , 1 , " name" )
54
- fmt.Println (j.String ())
55
-
56
- // Output:
57
- // Tom <nil>
58
- // [golang json] <nil>
59
- // [Tom Peter]
60
- // 1438194274 <nil>
61
- // false <nil>
62
- // Peter <nil>
63
-
49
+ j, _ = jsonport.Unmarshal ([]byte (jsonstr), " users" , 1 , " name" )
50
+ fmt.Println (j.String ()) // Peter, nil
64
51
}
65
-
66
-
67
52
```
68
53
69
54
For more information on getting started with ` jsonport ` [ check out the doc] ( https://godoc.org/github.com/xiaost/jsonport )
0 commit comments