Skip to content

Commit ec929ac

Browse files
committed
chore: updated README.md
1 parent 304b563 commit ec929ac

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

README.md

+15-30
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,38 @@ It is inspired by [jmoiron/jsonq](https://github.com/jmoiron/jsonq). Feel free t
1717
package main
1818

1919
import (
20-
"fmt"
21-
"github.com/xiaost/jsonport"
20+
"fmt"
21+
22+
"github.com/xiaost/jsonport"
2223
)
2324

2425
func main() {
2526
jsonstr := `{
2627
"timestamp": "1438194274",
2728
"users": [{"id": 1, "name": "Tom"}, {"id": 2, "name": "Peter"}],
28-
"keywords": ["golang", "json"]
29+
"keywords": ["golang", "json"],
30+
"status": 1
2931
}`
30-
j, _ := Unmarshal([]byte(jsonstr))
31-
32-
// Output: Tom
33-
fmt.Println(j.GetString("users", 0, "name"))
32+
j, _ := jsonport.Unmarshal([]byte(jsonstr))
3433

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
4137

4238
// try parse STRING as NUMBER
39+
fmt.Println(j.Get("timestamp").Int()) // 0, type mismatch: expected NUMBER, found STRING
4340
j.StringAsNumber()
44-
// Output: 1438194274
45-
fmt.Println(j.Get("timestamp").Int())
41+
fmt.Println(j.Get("timestamp").Int()) // 1438194274, nil
4642

4743
// convert NUMBER, STRING, ARRAY and OBJECT type to BOOL
44+
fmt.Println(j.GetBool("status")) // false, type mismatch: expected BOOL, found NUMBER
4845
j.AllAsBool()
49-
// Output: false
50-
fmt.Println(j.GetBool("status"))
46+
fmt.Println(j.GetBool("status")) // true, nil
5147

5248
// 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
6451
}
65-
66-
6752
```
6853

6954
For more information on getting started with `jsonport` [check out the doc](https://godoc.org/github.com/xiaost/jsonport)

0 commit comments

Comments
 (0)