如何解析 json 对象并打印特定值
How to parse json object and print particular value
我有 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)
}
我在做 soo 时遇到错误..
./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
对象。请帮我完成这个。
你快到了。解析整个文档,然后选择你想要的部分。
var results map[string][]interface{}
json.Unmarshal([]byte(empArray), &results)
fmt.Println(results["meta"])
您应该使用自定义结构:
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
Department string `json:"department"`
Designation string `json:"designation"`
}
type Employees struct {
Meta []Employee `json:"meta"`
}
当您尝试将提供的字符串解组为 Employees
var 时,它将读取注释并知道将每个字段放在哪里。您可以在 Golang Playground 找到工作示例。我在 Employee
结构中添加了一个字符串表示,以便 fmt.Println
输出更可读。
如果有一个额外的嵌套键({meta: {data: [...]}}
),类型如下:
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
Department string `json:"department"`
Designation string `json:"designation"`
}
type EmployeesData struct {
Data []Employee `json:"data"`
}
type Employees struct {
Meta EmployeesData `json:"meta"`
}
您也可以在 Golang Playground 找到工作示例。
注意: 我没有正确命名结构的上下文,所以我使用了 Employees
和 EmployeesData
但你应该使用更具描述性的名称帮助理解整个对象代表什么,而不仅仅是元和数据字段。
我有 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)
}
我在做 soo 时遇到错误..
./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
对象。请帮我完成这个。
你快到了。解析整个文档,然后选择你想要的部分。
var results map[string][]interface{}
json.Unmarshal([]byte(empArray), &results)
fmt.Println(results["meta"])
您应该使用自定义结构:
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
Department string `json:"department"`
Designation string `json:"designation"`
}
type Employees struct {
Meta []Employee `json:"meta"`
}
当您尝试将提供的字符串解组为 Employees
var 时,它将读取注释并知道将每个字段放在哪里。您可以在 Golang Playground 找到工作示例。我在 Employee
结构中添加了一个字符串表示,以便 fmt.Println
输出更可读。
如果有一个额外的嵌套键({meta: {data: [...]}}
),类型如下:
type Employee struct {
ID int `json:"id"`
Name string `json:"name"`
Department string `json:"department"`
Designation string `json:"designation"`
}
type EmployeesData struct {
Data []Employee `json:"data"`
}
type Employees struct {
Meta EmployeesData `json:"meta"`
}
您也可以在 Golang Playground 找到工作示例。
注意: 我没有正确命名结构的上下文,所以我使用了 Employees
和 EmployeesData
但你应该使用更具描述性的名称帮助理解整个对象代表什么,而不仅仅是元和数据字段。