https://github.com/guidao/jsonquery
another way to access json for go
https://github.com/guidao/jsonquery
golang json
Last synced: about 2 months ago
JSON representation
another way to access json for go
- Host: GitHub
- URL: https://github.com/guidao/jsonquery
- Owner: guidao
- Created: 2018-04-27T10:37:44.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-08T12:32:23.000Z (about 7 years ago)
- Last Synced: 2025-01-24T14:17:51.383Z (over 1 year ago)
- Topics: golang, json
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# jsonquery
一个方便的json值获取库, go的json解析方式要么需要预先定义结构,要么需要处理map[string]interface{}。第一种取值方便,解析麻烦。第二种解析时方便,取值时麻烦。而这个库综合了这两种方式的优点。
```go
package main
import (
"fmt"
jq "github.com/guidao/jsonquery"
)
func main() {
//error
v := jq.NewLens().Key("inner").GetWithJson(`{}`)
fmt.Println(v.StringOr("default"), v.Error())
//object
hello := jq.NewLens().Key("hello").GetWithJson(`{"hello": "world"}`).StringOr("")
fmt.Println(hello) // world
//array
f23, err := jq.NewLens().Key("array").Index(2).GetWithJson(`{"array":["hello", "world", 23]}`).Float64()
fmt.Println(f23, err) //23, nil
//foreach
err = jq.NewLens().Key("array").GetWithJson(`{"array":["hello", "world", 23]}`).ForeachArray(func(i int, v jq.Value) {
fmt.Printf("i:%v, v:%v\n", i, v.InterfaceOr(nil))
// i:0, v:hello
// i:1, v:world
// i:2, v:23
})
fmt.Println(err) //nil
}
```
更多例子参考test文件