API调用中的JSON解码不正确,我得到了一个空的结构体。

huangapple go评论97阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2016年4月17日 10:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/36671997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定