📌  相关文章
📜  Golang 示例中解析 json 数据对象 - Go 编程语言 - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:45:00.497000             🧑  作者: Mango

代码示例1
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    //Simple Employee JSON object which we will parse
    empJson := `{
        "id": 11,
        "name": "Irshad",
        "department": "IT",
        "designation": "Product Manager",
        "address": {
            "city": "Mumbai",
            "state": "Maharashtra",
            "country": "India"
        }
    }`

    // Declared an empty interface
    var result map[string]interface{}

    // Unmarshal or Decode the JSON to the interface.
    json.Unmarshal([]byte(empJson), &result)

    address := result["address"].(map[string]interface{})

    //Reading each value by its key
    fmt.Println("Id :", result["id"],
        "\nName :", result["name"],
        "\nDepartment :", result["department"],
        "\nDesignation :", result["designation"],
        "\nAddress :", address["city"], address["state"], address["country"])
}