1 回答

TA貢獻1828條經驗 獲得超3個贊
嘗試創建另一個結構體,其中有一個名為Sourcetype 的字段source。在下面的示例中,我將此稱為 struct outer。您的輸入應該是一個數組outer,您的結果應該是一個數組source。
像這樣的東西:
import (
"encoding/json"
"fmt"
)
var input = `[
{
"not needed": "",
"_source": {
"id": "id1",
"friendly": "friendly1"
}
},
{
"_source": {
"id": "id2",
"friendly": "friendly2"
}
}]`
type outer struct {
Source source `json:"_source"`
}
type source struct {
Id string `json:"id"`
Friendly string `json:"friendly"`
}
func main() {
result := make([]source, 0)
sources := []outer{}
json.Unmarshal([]byte(input), &sources)
for _, n := range sources {
result = append(result, n.Source)
}
out, _ := json.Marshal(result)
fmt.Println(string(out))
}```
- 1 回答
- 0 關注
- 199 瀏覽
添加回答
舉報