首页 > 解决方案 > 根据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 会覆盖默认值,所以这两种方法不能同时存在。我想知道如何解决这种将两种不同格式的数据解组到一个结构中的问题。

标签: jsongo

解决方案


使用您从 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

推荐阅读