You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+68-14Lines changed: 68 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,28 +23,79 @@ go get github.com/antchfx/jsonquery
23
23
24
24
## Get Started
25
25
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.
27
27
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
+
funcmain() {
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"}
0 commit comments