我看的相当于Document.parse()
在golang,这允许我直接从json创建bson?我不想为编组创建中间golang结构
The*_*ppo 12
该gopkg.in/mgo.v2/bson软件包有一个函数调用UnmarshalJSON,它完全符合您的要求.
该data参数应该将JSON字符串保存为[]byte值.
func UnmarshalJSON(data []byte, value interface{}) error
Run Code Online (Sandbox Code Playgroud)
UnmarshalJSON解组可能包含BSON扩展JSON规范中定义的非标准语法的JSON值.
例:
var bdoc interface{}
err = bson.UnmarshalJSON([]byte(`{"id": 1,"name": "A green door","price": 12.50,"tags": ["home", "green"]}`),&bdoc)
if err != nil {
panic(err)
}
err = c.Insert(&bdoc)
if err != nil {
panic(err)
}
Run Code Online (Sandbox Code Playgroud)
mongo-go-driver有一个功能bson.UnmarshalExtJSON可以完成这项工作。
这是示例:
var doc interface{}
err := bson.UnmarshalExtJSON([]byte(`{"foo":"bar"}`), true, &doc)
if err != nil {
// handle error
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7555 次 |
| 最近记录: |