douchen5971 2013-08-17 23:31
浏览 60
已采纳

解组反射值

Here is the code

package main

import (
    "fmt"

    "encoding/json"
    "reflect"
)

var (
    datajson []byte
    //ref mapp
)

type mapp map[string]reflect.Type

type User struct {
    Name string
    //Type map[string]reflect.Type
}

func MustJSONEncode(i interface{}) []byte {
    result, err := json.Marshal(i)
    if err != nil {
        panic(err)
    }
    return result
}
func MustJSONDecode(b []byte, i interface{}) {
    err := json.Unmarshal(b, i)
    if err != nil {
        panic(err)
    }

}
func Store(a interface{}) {
    datajson = MustJSONEncode(a)
    //fmt.Println(datajson)
}

func Get(a []byte, b interface{}) {
    objType := reflect.TypeOf(b).Elem()
obj := reflect.New(objType)
//fmt.Println(obj)
MustJSONDecode(a, &obj)
fmt.Printf("%s", obj)
    }

func main() {

    dummy := &User{}
    david := User{Name: "DavidMahon"}

    Store(david)
    Get(datajson, dummy)

}

In the Get function

func Get(a []byte, b interface{}) {
    objType := reflect.TypeOf(b).Elem()
obj := reflect.New(objType)
//fmt.Println(obj)
MustJSONDecode(a, &obj)
fmt.Printf("%s", obj)
    }

I am unable to unmarshal the json into the underlying object type.

Whats wrong here? I am so stuck here. Something very simple yet so difficult to figure out.

Thanks

UPDATE::Goal of this problem is to retreive a fully formed object of type passed in Get function.

The approach mentioned by Nick on the comment below doesnot get me the actual object which I already tried before. I can anyways retrieve the data (even when the object has recursive objects underneath) in a map like this

func Get(a []byte) {
    var f interface{}

    //buf := bytes.NewBuffer(a)
    //v := buf.String()
    //usr := &User{}

    MustJSONDecode(a, &f)
    fmt.Printf("
 %v 
", f)
}

However I need the actual object back not just the data. Something like user := &User{"SomeName"} where I need user object back from Unmarshall. The trick is somewhere in reflection but dont know how.

  • 写回答

2条回答 默认 最新

  • dongyi6183 2013-08-18 10:01
    关注

    I'm confused as to why you want to do this, but here is how to fix it

    func Get(a []byte, b interface{}) {
        objType := reflect.TypeOf(b).Elem()
        obj := reflect.New(objType).Interface()
        //fmt.Println(obj)
        MustJSONDecode(a, &obj)
        fmt.Printf("obj = %#v
    ", obj)
    }
    

    Note the call to Interface().

    Playground link

    It seems to me that you are going to a lot of trouble to make an empty &User when you already have one in b, eg

    func Get(a []byte, b interface{}) {
        MustJSONDecode(a, &b)
        fmt.Printf("obj = %#v
    ", b)
    }
    

    But I'm guessing there is some more to this plan which isn't apparent here!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计