说明
由于长期习惯 PHP 操作 json ,可以直接解析并静默处理异常返回一个默认值,不习惯 Go 需要先定义结构体再解析 json 的方式,于是有了此包。
使用示例
// 获取包:go get github.com/xing393939/jsonobject
jsonContent := `{"name":"John", "score":100}`
jo := jsonobject.NewJsonObject(jsonContent)
println(jo.GetString("name"), jo.GetInt("score"))
jsonContent = `[{"name":"John", "score":100}, {"name":"Tom", "score":200}]`
jo = jsonobject.NewJsonObject(jsonContent)
joArr := jo.GetJsonObjectSlice()
for _, joRow := range joArr {
println(joRow.GetString("name"), joRow.GetInt("score"))
}
类方法说明
类方法是可变参数,如果不传参数,就表示把当前 JsonObject 对象转换成对应的 string 、int 、bool 等;如果传一个参数 arg 则表示获取当前 JsonObject 对象元素键名为 arg 的键值。
| 类方法 | 如果类型不匹配 |
|---|---|
| GetString | 返回空字符 |
| GetInt | 返回 0 |
| GetBool | 返回 false |
| GetInt64 | 返回 0 |
| GetFloat64 | 返回 0 |
| GetInt64 | 返回 0 |
| GetJsonObject | * |
| GetJsonObjectSlice | 返回空切片 |