1 回答
TA贡献1856条经验 获得超11个赞
下面的代码应该可以满足您的需求:
package main
import (
"encoding/json"
"fmt"
)
var input = `{
"aaaa": 142,
"bbbb": "",
"cccc": {
"c1": 121,
"needed": [
{
"_c21": 22.25972,
"_c22": {
"needed1": "123",
"needed2": "test"
}
},
{
"_c21": 22.25972,
"_c22": {
"needed1": "123",
"needed2": "test"
}
},
{
"_c21": 22.25972,
"_c22": {
"needed1": "123",
"needed2": "test"
}
}
]
}
}`
type Json struct {
Cccc struct {
NeededList []struct {
C22 needed `json:"_c22"`
} `json:"needed"`
}`json:"cccc"`
}
type needed struct {
Needed1 string `json:"needed1"`
Needed2 string `json:"needed2"`
}
func main() {
j := &Json{}
json.Unmarshal([]byte(input), j)
result := make([]needed, 0)
for _, n := range j.Cccc.NeededList {
result = append(result, n.C22)
}
out, _ := json.Marshal(result)
fmt.Println(string(out))
}
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报