简体   繁体   中英

Go Unmarshal reflect.Type got map[string]interface{}

I want to convert json string to some struct ,use as func param
I use reflect to get Param Type,this work fine ,
but if I use json.Unmarshal I always get map[string]interface{}

this is a mini Run sample,File name is json_test.go

package testJson

import (
    "reflect"
    "encoding/json"
    "testing"
    "log"
)

type LoginForm struct {
    Name string
}

func Login(login LoginForm) {

}

func TestRun(t *testing.T) {
    _func := Login
    f := reflect.ValueOf(_func)
    funcType := f.Type()
    paramType := funcType.In(0)
    log.Print(paramType.Name())
    v := reflect.New(paramType).Elem().Interface()
    jsonstr := `{"Name":"123"}`
    log.Print(jsonstr)
    log.Print(reflect.TypeOf(v).Name())
    log.Print("%+v", v)
    err := json.Unmarshal([]byte(jsonstr), &v)
    if err != nil {
        panic(err)
    }
    log.Print(reflect.TypeOf(v).Name())
    log.Print( v)
    var in []reflect.Value
    in = make([]reflect.Value, funcType.NumIn())
    in[0] = reflect.ValueOf(v)
    f.Call(in)
}

Thanks for your help!

Don't reflect.New a pointer type because the result of that is a pointer to the pointer, instead reflect.New the Elem of the pointer type.

Change this:

v := reflect.New(paramType).Elem().Interface()

To this:

v := reflect.New(paramType.Elem()).Interface()

At this point v is already a pointer to LoginForm so you don't need to get its address with & when passing to Unmarshal .

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


Also the reason you got map[string]interface{} is because the return type of the Interface() method is unsurprisingly enough interface{} , so while v 's underlying type could be anything its "topmost" type is interface{} . So then, doing &v will get you *interface{} as opposed to the inteded *LoginForm . And in the docs you can find that trying to unmarshal a JSON object into an interface{} will result in a value of type map[string]interface{} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM