我编写了以下代码以从 JSON 获取数组,并希望检索类似
[{"id":"id1","friendly":"friendly1"},{"id":"id2","friendly":"friendly2"}]
但它是空的:
[{"id":"","friendly":""},{"id":"","friendly":""}]
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))
}
慕勒3428872
相关分类