Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created September 25, 2017 07:07
Show Gist options
  • Save leizongmin/acc9f5363ffbb5af10724a0a26494e60 to your computer and use it in GitHub Desktop.
Save leizongmin/acc9f5363ffbb5af10724a0a26494e60 to your computer and use it in GitHub Desktop.
golang JSON
package main
import (
"fmt"
"encoding/json"
"github.com/mitchellh/mapstructure"
)
type A struct {
A int
B int
Hello string
}
func main() {
var str = `
{
"a": 123,
"b": 456,
"c": {
"d": true,
"e": null
},
"e": [ 123, 456, { "x": { "y": 444}} ],
"HeLLO": "hello"
}
`
var data map[string]interface{}
json.Unmarshal([]byte(str), &data)
fmt.Println(data)
if b, err := json.Marshal(data); err != nil {
fmt.Println(err)
} else {
fmt.Println(string(b))
}
var a A
if err := mapstructure.Decode(data, &a); err != nil {
fmt.Println(err)
} else {
fmt.Println(a)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment