如何使用 Paths 获取所有匹配项
package main
import (
"fmt"
"GitHub.com/tidwall/gjson"
)
func main() {
ret := []byte({ "result": { "data": [ {"data": [{"type": "quan", "value": 1}, {"type": "quan", "value": 2}, {"type": "quan", "value": 3}, {"type": "other", "value": 4}]}, {"data": [{"type": "quan", "value": 3}]} ] } })
path := "result.data.#.data.#(type=="quan")"
result := gjson.GetBytes(ret, path)
fmt.Println(result, result.Paths(string(ret)))
path = "result.data.#.data.#(type=="quan")#"
result = gjson.GetBytes(ret, path)
fmt.Println(result, result.Paths(string(ret)))
}
输出
[{"type": "quan", "value": 1},{"type": "quan", "value": 3}] [result.data.0.data.0 result.data.1.data.0]
[[{"type": "quan", "value": 1},{"type": "quan", "value": 2},{"type": "quan", "value": 3}],[{"type": "quan", "value": 3}]] [ ]
内容来源: tidwall/gjson