英文:
JSON in API call is not decoding correctly, I am getting a blank struct
问题
我的JSON看起来是这样的:
{
"website": {
"id": 8,
"account_id": 9,
"name": "max",
"website_url": "",
"subscription_status": "trial",
"created_at": "2016-01-24T01:43:41.693Z",
"updated_at": "2016-02-21T01:17:53.129Z"
}
}
我的Website结构如下:
type Website struct {
Id int64 json:"id"
AccountId int64 json:"account_id"
Name string json:"name"
WebsiteUrl string json:"website_url"
SubscriptionStatus string json:"subscription_status"
CreatedAt time.Time json:"created_at"
UpdatedAt time.Time json:"updated_at"
}
然后我有一个Company结构如下:
type Company struct {
Website Website
api *API
}
所以在我的API客户端代码中,我有这样的代码:
res, status, err := api.request(endpoint, "GET", nil, nil)
if err != nil {
return nil, err
}
if status != 200 {
return nil, fmt.Errorf("Status returned: %d", status)
}
r := map[string]Company{}
err = json.NewDecoder(res).Decode(&r)
fmt.Printf("things are: %v\n\n", r)
我得到的输出是:
things are: map[website:{{0 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC}
我不确定为什么它没有正确设置Website结构的值,我该如何调试这个问题?
更新
我添加了以下代码,但没有看到任何输出:
if err != nil {
fmt.Printf("error is not nil!!!:")
fmt.Println(err)
return nil, err
}
英文:
My JSON looks like this:
{
"website": {
"id": 8,
"account_id": 9,
"name": "max",
"website_url": "",
"subscription_status": "trial",
"created_at": "2016-01-24T01:43:41.693Z",
"updated_at": "2016-02-21T01:17:53.129Z",
}
}
My Website struct looks like this:
type Website struct {
Id int64 `json:"id"`
AccountId int64 `json:"account_id"`
Name string `json:"name"`
WebsiteUrl string `json:"website_url"`
SubscriptionStatus string `json:"subscription_status"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
I then have a Company struct like:
type Company struct {
Website Website
api *API
}
So in my API client code I have this:
res, status, err := api.request(endpoint, "GET", nil, nil)
if err != nil {
return nil, err
}
if status != 200 {
return nil, fmt.Errorf("Status returned: %d", status)
}
r := map[string]Company{}
err = json.NewDecoder(res).Decode(&r)
fmt.Printf("things are: %v\n\n", r)
The output I am getting is this:
things are: map[website:{{0 0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC} <nil>}]
How can I debug this, I am not sure why it is not setting the Website struct with correct values.
Update
I added this, I don't see any output:
if err != nil {
fmt.Printf("error is not nillllllll!!!:")
fmt.Println(err)
return nil, err
}
答案1
得分: 2
因为你在map
-> Company
-> Website
这个层级嵌套太多了。map
实际上在其上面又建立了另一层,所以它期望有三层嵌套的JavaScript对象。实际上,你提供的JSON只有两层,即Company
-> Website
。
所以有几种方法可以使其工作,选择适合你的方法。
你可以这样做:
r := map[string]Website{}
json.NewDecoder(res).Decode(&r)
或者你可以这样做:
r := Company{}
json.NewDecoder(res).Decode(&r)
英文:
Its because you have too many levels of nesting with the map
> Company
> Website
. The map
effectively builds another layer on top of it so its expecting three levels of JavaScript objects nested. In actuality, the JSON you provided only has two with Company
> Website
.
So there are a couple of ways to get it to work; choose the one that works best for you.
You can do:
r := map[string]Website{}
json.NewDecoder(res).Decode(&r)
Or you can do:
r := Company{}
json.NewDecoder(res).Decode(&r)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论