如果可以unmarshal将数据放入结构体中,则可以使用以下代码:
package main
import (
"encoding/json"
"fmt"
)
type DataStructure struct {
Key string `json:"Key"`
Record struct {
Name string `json:"name"`
Type string `json:"type"`
Validity bool `json:"validity"`
} `json:"Record"`
}
var data string = `[{"Key":"area1", "Record": {"name":"belfast","type":"surburban","validity":true}},{"Key":"area1", "Record": {"name":"belfast","type":"surburban","validity":false}}]`
func main() {
var datastruct []DataStructure
var result []DataStructure
if err := json.Unmarshal([]byte(data), &datastruct); err != nil {
panic(err)
}
for _, item := range datastruct {
if item.Record.Validity {
result = append(result, item)
}
}
fmt.Println(result)
}
输入:
[{"Key":"area1", "Record": {"name":"belfast","type":"surburban","validity":true}},{"Key":"area1", "Record": {"name":"belfast","type":"surburban","validity":false}}]
预期结果:
[{Key:area1 Record:{Name:belfast Type:surburban Validity:true}}]
江户川乱折腾
皈依舞
相关分类