Gabs 是一个用来处理动态或未知json结构的golang库。通过包装json.Marshal/json.Unmarshal的行为和map[string]interface{} 对象,Gabs提供了更大的便利性。
![image](https://static.oschina.net/uploads/space/2016/0916/125723_GcnI_2432165.png)
### 解析和搜索 JSON
<pre class="brush:cpp; toolbar: true; auto-links: false;">...
import "github.com/Jeffail/gabs"
jsonParsed, err := gabs.ParseJSON([]byte(`{
"outter":{
"inner":{
"value1":10,
"value2":22
},
"alsoInner":{
"value1":20
}
}
}`))
var value float64
var ok bool
value, ok = jsonParsed.Path("outter.inner.value1").Data().(float64)
// value == 10.0, ok == true
value, ok = jsonParsed.Search("outter", "inner", "value1").Data().(float64)
// value == 10.0, ok == true
value, ok = jsonParsed.Path("does.not.exist").Data().(float64)
// value == 0.0, ok == false
exists := jsonParsed.Exists("outter", "inner", "value1")
// exists == true
exists := jsonParsed.Exists("does", "not", "exist")
// exists == false
exists := jsonParsed.ExistsP("does.not.exist")
// exists == false
...</pre>