json - 根据golang中的条件执行自我解组方法或默认解组方法
问题描述
我是golang的新手。我有一个结构Item。
type Item Struct{
...
}
我知道它有一个默认的UnmarshalJSON方法。
现在我想将数据解组到它。
对于数据可能有两种不同的格式。所以我的期望如下:
if condition {
//execute default UnmarshalJSON
json.Unmarshal(data, &item)
}else{
//execute my own UnmarshalJSON
json.Unmarshal(data, &item)
}
这是我自己的 UnmarshalJSON。
func (item *Item) UnmarshalJSON(data []byte) error{
...
}
也许我自己 UnmarshalJSON 会覆盖默认值,所以这两种方法不能同时存在。我想知道如何解决这种将两种不同格式的数据解组到一个结构中的问题。
解决方案
使用您从 json 响应中获得的任何格式的接口,并将响应解组为接口:
func main(){
var result interface{}
if err := json.Unmarshal(jsonbytes, &result); err != nil{
fmt.Println(err)
}
}
然后使用类型断言来获取接口底层的值。但是我认为在您的情况下,如果您没有密钥的基础类型。更好的是使用递归来获取值。
func fetchValue(value interface{}) {
switch value.(type) {
case string:
fmt.Printf("%v is an interface \n ", value)
case bool:
fmt.Printf("%v is bool \n ", value)
case float64:
fmt.Printf("%v is float64 \n ", value)
case []interface{}:
fmt.Printf("%v is a slice of interface \n ", value)
for _, v := range value.([]interface{}) { // use type assertion to loop over []interface{}
fetchValue(v)
}
case map[string]interface{}:
fmt.Printf("%v is a map \n ", value)
for _, v := range value.(map[string]interface{}) { // use type assertion to loop over map[string]interface{}
fetchValue(v)
}
default:
fmt.Printf("%v is unknown \n ", value)
}
}
Go 操场上的工作代码
上面的代码将允许您获取解析为接口的任何类型的值。
笔记:
在 golang 中,定义了当您将未知 json 解组到接口中时。它将被转换为以下类型:
bool, for JSON booleans
float64, for JSON numbers
string, for JSON strings
[]interface{}, for JSON arrays // slice of interface{}
map[string]interface{}, for JSON objects
nil for JSON null
推荐阅读
- r - 如何在 for 循环内的名称-值对中指定名称?
- r - R中的时间和日期问题
- php - 静态变量设置为空,然后使用 if 语句检查该变量是否为空
- android - 如何在 Go 和 Android 之间使用 RSA
- delphi - 如何在一条指令中创建具有已定义元素的字符串数组?
- reactjs - 取一个字符串但需要反应组件
- java - 原因:android.view.InflateException: Binary XML file line #11: Error inflating class io.flutter.view.FlutterView
- c# - 如何构建像 Value = ClassName.Value 这样的脚本结构?
- reactjs - 测试运行完成后一秒 Jest 没有退出
- java - Feign 客户端:发布地图
在请求正文 => feign.FeignException:状态 400 读取 MAp