Skip to content

Commit e55c8e3

Browse files
authored
Merge pull request #11 from 1Mark/improve_readme
Add clear examples of jsonquery to README.md
2 parents f17dd2b + fdb7509 commit e55c8e3

1 file changed

Lines changed: 68 additions & 14 deletions

File tree

README.md

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,79 @@ go get github.com/antchfx/jsonquery
2323

2424
## Get Started
2525

26-
The below code may be help your understand what it does. We don't need pre-defined strcutre or using regexp to extract some data in JSON file, gets any data is easy and fast in jsonquery now.
26+
The below code may be help your understand what it does. We don't need pre-defined structure or using regexp to extract some data in JSON file, gets any data is easy and fast in jsonquery now.
2727

28+
29+
Using an xpath like syntax to access specific fields of a json structure.
30+
```go
31+
// https://go.dev/play/p/vqoD_jWryKY
32+
package main
33+
34+
import (
35+
"fmt"
36+
"strings"
37+
38+
"github.com/antchfx/jsonquery"
39+
)
40+
41+
func main() {
42+
s := `{
43+
"person":{
44+
"name":"John",
45+
"age":31,
46+
"female":false,
47+
"city":null,
48+
"hobbies":[
49+
"coding",
50+
"eating",
51+
"football"
52+
]
53+
}
54+
}`
55+
doc, err := jsonquery.Parse(strings.NewReader(s))
56+
if err != nil {
57+
panic(err)
58+
}
59+
// xpath query
60+
age := jsonquery.FindOne(doc, "age")
61+
// or
62+
age = jsonquery.FindOne(doc, "person/age")
63+
fmt.Printf("%#v[%T]\n", age.Value(), age.Value()) // prints 31[float64]
64+
65+
hobbies := jsonquery.FindOne(doc, "//hobbies")
66+
fmt.Printf("%#v\n", hobbies.Value()) // prints []interface {}{"coding", "eating", "football"}
67+
firstHobby := jsonquery.FindOne(doc, "//hobbies/*[1]")
68+
fmt.Printf("%#v\n", firstHobby.Value()) // "coding"
69+
}
70+
71+
Iterating over a json structure.
2872
```go
29-
s := `{
73+
// https://go.dev/play/p/vwXQKTCLdVK
74+
package main
75+
76+
import (
77+
"fmt"
78+
"strings"
79+
80+
"github.com/antchfx/jsonquery"
81+
)
82+
83+
func main() {
84+
s := `{
3085
"name":"John",
3186
"age":31,
3287
"female":false,
3388
"city":null
3489
}`
35-
doc, err := jsonquery.Parse(strings.NewReader(s))
36-
if err != nil {
37-
panic(err)
38-
}
39-
// iterate all json objects from child ndoes.
40-
for _, n := range doc.ChildNodes() {
41-
fmt.Printf("%s: %v[%T]\n", n.Data, n.Value(), n.Value())
90+
doc, err := jsonquery.Parse(strings.NewReader(s))
91+
if err != nil {
92+
panic(err)
93+
}
94+
// iterate all json objects from child ndoes.
95+
for _, n := range doc.ChildNodes() {
96+
fmt.Printf("%s: %v[%T]\n", n.Data, n.Value(), n.Value())
97+
}
4298
}
43-
// xpath query
44-
n := jsonquery.FindOne(doc, "//age")
45-
fmt.Printf("age: %.2f", n.Value().(float64))
46-
// select a child node with `age`. = jsonquery.FindOne(doc,"age")
47-
m := doc.SelectElement("age")
4899
```
49100

50101
// Output:
@@ -56,6 +107,9 @@ female: false[bool]
56107
city: <nil>[<nil>]
57108
```
58109
110+
```
111+
112+
59113
The default Json types and Go types are:
60114

61115
| JSON | jsonquery(go) |

0 commit comments

Comments
 (0)