Skip to content

Instantly share code, notes, and snippets.

@cipepser
Last active July 29, 2017 04:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cipepser/2bd83a17dba79bec793e5f07296e7b6e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"gopkg.in/mgo.v2/bson"
)
func main() {
var b interface{}
j := []byte(`{"id": 0, "name": "Alice", "countory": "Japan"}`)
// transform json to bson
err := bson.UnmarshalJSON(j, &b)
if err != nil {
log.Fatal(err)
}
fmt.Println(b) // map[countory:Japan id:0 name:Alice]
// transform bson to json
j2, err := bson.MarshalJSON(b)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j2)) // {"countory":"Japan","id":0,"name":"Alice"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment