我想了解一下如何将嵌套的响应数据展开到自定义结构中。下面是一个返回给我的数据示例。我正在尝试获取用户数据。
{
"_links": {
"self": {
"href": "/api/v2/user-search/default/test?after=1585612800000&limit=20&offset=0&q=johnsmith%40test.com",
"type": "application/json"
}
},
"totalCount": 1,
"items": [
{
"lastPing": "2020-04-30T02:56:10.430867577Z",
"environmentId": "xxxx",
"ownerId": "xxxx",
"user": {
"key": "johnsmith@test.com",
"email": "johnsmith@test.com",
"firstName": "john",
"lastName": "smith"
},
"_links": {
"parent": {
"href": "/api/v2/users/default/test",
"type": "application/json"
},
"self": {
"href": "/api/v2/users/default/test/johnsmith@test.com",
"type": "application/json"
},
"settings": {
"href": "/api/v2/users/default/test/johnsmith@test.com/flags",
"type": "text/html"
},
"site": {
"href": "/default/test/users/johnsmith@test.com",
"type": "text/html"
}
}
}
]
}
目前我正在做以下
respData := map[string][]map[string]map[string]interface{}{}
json.Unmarshal(respBody, &respData)
userData := respData["items"][0]["user"]
我很想把它分解成一个自定义结构,但我似乎无法让它发挥作用。用户对象所在的嵌套切片一直让我很反感
type User struct {
Key string `json:"key"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type LinkInfo struct {
Href string `json:"href"`
Type string `json:"type"`
}
type Item struct {
LastPing time.Time `json:"lastPing"`
EnvironmentID string `json:"environmentId"`
OwnerID string `json:"ownerId"`
User User `json:"user"`
Links LinkInfo `json:"_links"`
Self LinkInfo `json:"self"`
Settings LinkInfo `json:"settings"`
Parent LinkInfo `json:"parent"`
Site LinkInfo `json:"site"`
}
type ItemDetails struct {
Links LinkInfo `json:"_links"`
TotalCount int `json:"total_count"`
Items []Item
}
你能试试这个吗?
https://play.golang.org/p/S_CUN0XEh-d
从你提到的内容来看,你似乎走在了正确的轨道上。您的JSON相当大,所以让我给您举一个较小的例子,类似于您提到的遇到问题的部分(items
列表中的user
对象(。
type response struct {
TotalCount int `json:"totalCount"`
Items []*itemStruct `json:"items"`
}
type itemStruct struct {
LastPing string `json:"lastPing"`
User *userStruct `json:"user"`
}
type userStruct struct {
Key string `json:"key"`
}
基本上,要映射到对象的JSON列表,只需在结构中放入这样的字段:Objects []*structWhichMapsToMyObject
编辑:以下是Go Playground中运行的代码:https://play.golang.org/p/EvSvv-2s8y8
如果你想要这个:
"user": {
"key": "johnsmith@test.com",
"email": "johnsmith@test.com",
"firstName": "john",
"lastName": "smith"
}
声明一个匹配的Go结构:
type User struct {
Key string `json:"key"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
然后,由于用户的父对象看起来像这样:
"items": [
{
"lastPing": "2020-04-30T02:56:10.430867577Z",
"environmentId": "xxxx",
"ownerId": "xxxx",
"user": { ... },
"_links": { ... }
}
]
您还需要为此声明一个匹配的Go结构(您可以省略不需要的字段(:
type Item struct {
User User `json:"user"`
}
然后是父项的父项:
{
"_links": {
"self": {
"href": "/api/v2/user-search/default/test?after=1585612800000&limit=20&offset=0&q=johnsmith%40test.com",
"type": "application/json"
}
},
"totalCount": 1,
"items": [ ... ]
}
同样,祖父母的匹配Go结构只包括您需要的字段:
type ResponseData struct {
Items []Item `json:"items"`
}
一旦有了这个,就可以将json解码为ResponseData
:的实例
var rd ResponseData
if err := json.Unmarshal(data, &rd); err != nil {
panic(err)
}
for _, item := range rd.Items {
fmt.Println(item.User)
}
https://play.golang.com/p/7yavVSBcHQP