douyi3676 2017-02-22 07:42
浏览 471
已采纳

MongoDB使用bson.Raw从查询返回整个JSON

I'm trying to search for this document:

"meta": {
    "pageId": "...",
    "userId": "...",
    "ver": "0",
},
"dialog": {
 ...

 }

and get the entire "dialog" as a JSON, so I created this struct:

type Dialog struct {
    Dialog bson.Raw `json:"dialog" bson:"dialog"`
}

So I query the document like this:

dialog := Dialog{}
query := c.Find(locate).One(&dialog)

and when I print dialog, I get a bunch of numbers, which I believe are the raw bytes from the query.

The question is: how to unmarshal it into a JSON object?

The only thing I've found about this are Marshal into a bson.Raw (which doesn't explain how to unmarshal into a json)

Update

Following How to marshal json string to bson document in golang for writing to MongoDB?, I did:

fmt.Println(bson.UnmarshalJSON(dialog.Dialog.Data, &a))

which gets me:

json: unknown constant "l"

As you can see I had to extract the Data from the Raw type, I don't think this is the best way to do it since there's the Kind field which is not being used. Also, what's this 'l'?

Update 2

I thought I had to Unmarshal into a JSON type in order to work with it but I've found that's better to Unmarshal to a map directly, so here it is:

var a bson.M
fmt.Println(bson.Unmarshal(dialog.Dialog.Data, &a))
fmt.Println(a)

It worked for me :)

However, I'm still ignoring the Kind field of the Raw type. Is there a better way to do it?

  • 写回答

1条回答 默认 最新

  • douxing2156 2017-02-22 08:37
    关注

    JSON is not a type, so you can't unmarshal into a value of type JSON. JSON is a textual representation of some structured data.

    bson.Raw is also not equal to JSON representation, so some kind of transformation is inevitable.

    What you may do is unmarshal into a value of type interface{}, and then use json.Marshal() to "render" the JSON representation.

    If you want this to be "automatic", you may create a new type called JSONStr, and you may implement the bson.Setter and bson.Getter interfaces by it.

    This is how it could look like:

    type JSONStr string
    
    func (j *JSONStr) SetBSON(raw bson.Raw) (err error) {
        var i interface{}
        if err = raw.Unmarshal(&i); err != nil {
            return
        }
        data, err := json.Marshal(i)
        if err != nil {
            return
        }
        *j = JSONStr(data)
        return
    }
    
    func (j *JSONStr) GetBSON() (interface{}, error) {
        var i interface{}
        if err := json.Unmarshal([]byte(*j), &i); err != nil {
            return nil, err
        }
        return i, nil
    }
    

    And using this JSONStr type:

    type Dialog struct {
        Dialog JSONStr `bson:"dialog"`
    }
    

    Update:

    If you don't really want a JSON text representation, just use type interface{}:

    type Dialog struct {
        Dialog interface{} `bson:"dialog"`
    }
    

    And obtain the JSON text like this:

    var dialog Dialog
    // load a dialog
    data, err := json.Marshal(dialog.Dialog)
    if err != nil {
        // handle error
    }
    fmt.Println(string(data))
    

    (Basically this is what the JSONStr.SetBSON() method did exactly.)

    Note that interface{} will "cover" all data structures. If you know it's an object, you may use a map. If you know it's an array, you may use a slice, etc. You may also use any concrete type.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题