如何解析json对象并打印特定值[duplicate]

问题描述 投票:-2回答:2

我有由数组的子对象组成的json对象。如何在json中打印特定的子对象。这是我的代码

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    //Simple Employee JSON which we will parse
    empArray := `{"meta":[
        {
            "id": 1,
            "name": "Mr. Boss",
            "department": "",
            "designation": "Director"
        },
        {
            "id": 11,
            "name": "Irshad",
            "department": "IT",
            "designation": "Product Manager"
        },
        {
            "id": 12,
            "name": "Pankaj",
            "department": "IT",
            "designation": "Team Lead"
        }
    ]}`

    // Declared an empty interface of type Array
    var results []map[string]interface{}

    // Unmarshal or Decode the JSON to the interface.
    json.Unmarshal([]byte(empArray['meta']), &results)

    fmt.Println(results)
}

我在执行操作时遇到错误。.>

./test.go:35:23: cannot convert empArray['\u0000'] (type byte) to type []byte
./test.go:35:33: invalid character literal (more than one character)

empArray数组对象中,我想打印由雇员数组组成的meta对象。请帮助我完成此任务。

我有由数组的子对象组成的json对象。如何在json中打印特定的子对象。这是我的代码包主要导入(“ encoding / json”“ fmt”)func main(){// ...

arrays json go marshalling
2个回答
2
投票

您快到了。解析整个文档,然后选择所需的部分。


2
投票

您应该使用自定义结构:

© www.soinside.com 2019 - 2024. All rights reserved.