dou91855 2017-10-07 17:26
浏览 88
已采纳

解组map [string] interface {}时发生mgo错误

I want to store an arbitrary json object in a struct:

type C struct {
  Name string `json:"name" bson:"name"`
  Config map[string]interface{} `json:"config" bson:"config"`
}

This works fine when I store any deeply nested json object, but when I go retrieve it and mgo tries to Unmarshal it, I get

Unmarshal can't deal with struct values. Use a pointer.

I'm not sure what's supposed to be a pointer. If I change it to

Config *map[string]interface{}

the same error occurs.

The error occurs here: https://github.com/MG-RAST/golib/blob/master/mgo/bson/bson.go#L493

I don't know what it's reflecting on though.

  • 写回答

1条回答 默认 最新

  • duanjianl183188 2017-10-07 19:30
    关注

    So when you are unmarshaling the input argument takes a pointer to the struct, and you need to define a type in order to use a pointer to a struct.

    type myMap map[string]interface{}
    

    Then you can make a pointer to that type notice the ampersand to indicate pointer to your struct for type myMap, with json you could do something like so:

    json := []Byte{`{"name": "value"}`}
    c := &myMap{"value": "name"}
    json.Unmarshal(c, json)
    

    So you need *myMap to the struct not a pointer to the type. In order to explain the specific solution to this problem you need to add the context of how mongodb is unmarshaling your json.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致