在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:antchfx/jsonquery开源软件地址:https://github.com/antchfx/jsonquery开源编程语言:Go 100.0%开源软件介绍:jsonqueryOverviewjsonquery is XPath query package for JSON document depended on xpath package, writing in go. jsonquery helps you easy to extract any data from JSON using XPath query without using pre-defined object structure to unmarshal in go, saving your time. Install Package
Get StartedThe 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. s := `{
"name":"John",
"age":31,
"female":false,
"city":null
}`
doc, err := jsonquery.Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
// iterate all json objects from child ndoes.
for _, n := range doc.ChildNodes() {
fmt.Printf("%s: %v[%T]\n", n.Data, n.Value(), n.Value())
}
// xpath query
n := jsonquery.FindOne(doc, "//age")
fmt.Printf("age: %.2f", n.Value().(float64))
// select a child node with `age`. = jsonquery.FindOne(doc,"age")
m := doc.SelectElement("age") // Output:
The default Json types and Go types are:
For more information about JSON & Go see the https://go.dev/blog/json Getting StartedLoad JSON from URL.doc, err := jsonquery.LoadURL("http://www.example.com/feed?json") Load JSON from string.s :=`{
"name":"John",
"age":31,
"city":"New York"
}`
doc, err := jsonquery.Parse(strings.NewReader(s)) Load JSON from io.Reader.f, err := os.Open("./books.json")
doc, err := jsonquery.Parse(f) Parse JSON arrays := `[1,2,3,4,5,6]`
doc, _ := jsonquery.Parse(strings.NewReader(s))
list := jsonquery.Find(doc, "*")
for _, n := range list {
fmt.Print(n.Value().(float64))
} // Output: Convert JSON object to XMLdoc, _ := jsonquery.Parse(strings.NewReader(s))
fmt.Println(doc.OutputXML()) MethodsFindOne()n := jsonquery.FindOne(doc,"//a") Find()list := jsonquery.Find(doc,"//a") QuerySelector()n := jsonquery.QuerySelector(doc, xpath.MustCompile("//a")) QuerySelectorAll()list :=jsonquery.QuerySelectorAll(doc, xpath.MustCompile("//a")) Query()n, err := jsonquery.Query(doc, "*") QueryAll()list, err := jsonquery.QueryAll(doc, "*") Query() vs FindOne()
OutputXML()Convert current JSON object to XML format. Examples{
"store": {
"book": [
{
"id": 1,
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"id": 2,
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"id": 3,
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"id": 4,
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Tests
For more supports XPath feature and function see https://github.com/antchfx/xpath |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论