我编写了以下代码来从 JSON 获取数组,并且想要检索类似的内容
[{“id”:“id1”,“友好”:“友好1”},{“id”:“id2”,“友好”:“友好2”}]
但它是空的:
[{"id":"","friend":""},{"id":"","friend":""}]
package main
import (
"encoding/json"
"fmt"
)
var input = `[
{
"not needed": "",
"_source": {
"id": "id1",
"friendly": "friendly1"
}
},
{
"_source": {
"id": "id2",
"friendly": "friendly2"
}
}]`
type source struct {
Id string `json:"id"`
Friendly string `json:"friendly"`
}
func main() {
result := make([]source, 0)
sources := []source{}
json.Unmarshal([]byte(input), &sources)
for _, n := range sources {
result = append(result, n)
}
out, _ := json.Marshal(result)
fmt.Println(string(out))
}
子衿沉夜
相关分类