// JsonTest project main.go
package main
import (
"encoding/json"
"fmt"
"os"
)
var JSON = `{
"name":"张三",
"jobtitle" : "软件开发工程师",
"phone" : {
"home" : "123-4567-8901",
"office": "010-12345678"
},
"email":"zhangsan@163.com"
}`
func main() {
var info map[string]interface{}
err := json.Unmarshal([]byte(JSON), &info)
if err != nil {
fmt.Println("json Unmarshal error: ", err.Error())
os.Exit(-1)
}
fmt.Println("Name : ", info["name"])
fmt.Println("jobtitle : ", info["jobtitle"])
fmt.Println("phone.home : ", info["phone"].(map[string]interface{})["home"])
fmt.Println("phone.office : ", info["phone"].(map[string]interface{})["office"])
fmt.Println("email :", info["email"])
}
编译输出:
C:/Go/bin/go.exe build -i [D:/golang/src/JsonTest]
成功: 进程退出代码 0.
D:/golang/src/JsonTest/JsonTest.exe [D:/golang/src/JsonTest]
Name : 张三
jobtitle : 软件开发工程师
phone.home : 123-4567-8901
phone.office : 010-12345678
email : zhangsan@163.com
成功: 进程退出代码 0.