Created
September 25, 2017 07:07
-
-
Save leizongmin/acc9f5363ffbb5af10724a0a26494e60 to your computer and use it in GitHub Desktop.
golang JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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