An open API service indexing awesome lists of open source software.

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

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文件