dsag14654 2017-06-27 11:37
浏览 279
已采纳

如何告诉json.Unmarshal使用struct而不是接口

I want to write a function that receives several types of structs and unmarshals them from JSON. To this end, I have another set of functions with a pre-defined signature that return the struct instances but since each function returns a different type of struct the function signature has interface{} as the return type.

When I send json.Unmarshal a concrete struct it works as I expected but when I send the same struct as interface{} it converts it to a map.

Here is a simplified example code that depicts the problem:

package main

import (
"encoding/json"
    "fmt"
)

type Foo struct {
    Bar string `json:"bar"`
}  

func getFoo() interface{} {
    return Foo{"bar"}
}

func main() {

    fooInterface := getFoo() 
    fooStruct := Foo{"bar"}
    fmt.Println(fooInterface) //{bar}
    fmt.Println(fooStruct)    //{bar}

    myJSON := `{"bar":"This is the new value of bar"}`
    jsonBytes := []byte(myJSON)

    err := json.Unmarshal(jsonBytes, &fooInterface )
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(fooInterface) //map[bar:This is the new value of bar]

    err = json.Unmarshal(jsonBytes, &fooStruct)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(fooStruct) //{This is the new value of bar}
}

https://play.golang.org/p/tOO7Ki_i4c

I expected json.Unmarshal to use the concrete struct behind the interface for unmarshaling but it doesn't and just assigns the map of values to the passed interface.

Why doesn't it use the concrete struct and is there a way to tell it to use the concrete struct type without explicit casting (I don't know the explicit type at design time)?

  • 写回答

1条回答 默认 最新

  • dongyunwei8596 2017-06-27 11:41
    关注

    The encoding/json package can't magically guess what type you want the result unmarshaled into, unless you tell it to.

    One way of telling what to unmarsal into is to pass value of that type to the json.Unmarshal() function.

    And unfortunately there is no other way. If you pass a value of interface{} type, the json package implementation is free to choose a type of its choice, and it will choose map[string]interface{} for JSON objects, and []interface{} for JSON arrays. This is documented at json.Unmarshal():

    To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

    bool, for JSON booleans
    float64, for JSON numbers
    string, for JSON strings
    []interface{}, for JSON arrays
    map[string]interface{}, for JSON objects
    nil for JSON null
    

    If you know the type beforehand, create a value of that type, and pass that for unmarshaling. Whether your store this in an interface{} variable beforehand does not matter; if the passed value is suitable for unmarshaling, it will be used. Note that the passed value will be wrapped in an interface{} if not already of that type, as that is the parameter type of json.Unmarshal().

    The problem why your code fails is because you pass a value of type *interface{} which wraps a non-pointer Foo value. Since the json package can't use this, it creates a new value of its choice (a map).

    Instead you should wrap a *Foo value in an interface{}, and pass that:

    func getFoo() interface{} {
        return &Foo{"bar"}
    }
    
    func main() {
        fooInterface := getFoo()
    
        myJSON := `{"bar":"This is the new value of bar"}`
        jsonBytes := []byte(myJSON)
    
        err := json.Unmarshal(jsonBytes, fooInterface)
        if err != nil {
            fmt.Println(err)
        }
        fmt.Printf("%T %+v", fooInterface, fooInterface)
    }
    

    This results in (try it on the Go Playground):

    *main.Foo &{Bar:This is the new value of bar}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同