无法取消组合golang响应



我一直试图通过将JSON响应分解为结构来提取一些JSON,但我不知道为什么它做得不好。我也试过gjson,但结果相同。我是不是遗漏了什么?

JSON结果:

{"availabilities":[{"pickup":{"status":"OnlineOnly","purchasable":false},"shipping":{"status":"InStockOnlineOnly","purchasable":true},"sku":"12341231","sellerId":"438178","saleChannelExclusivity":"OnlineOnly","scheduledDelivery":false,"isGiftCard":false,"isService":false}]}

代码:

// Inventory ...
type Inventory struct {
Availabilities []Availability `json:"availabilities"`
}
// Availability ...
type Availability struct {
Sku                    string   `json:"sku"`
SellerID               string   `json:"sellerId"`
SaleChannelExclusivity string   `json:"saleChannelExclusivity"`
ScheduledDelivery      bool     `json:"scheduledDelivery"`
IsGiftCard             bool     `json:"isGiftCard"`
IsService              bool     `json:"isService"`
Pickup                 Statuses `json:"pickup"`
Shipping               Statuses `json:"shipping"`
}
// Statuses ..
type Statuses struct {
Status      string `json:"status"`
Purchasable bool   `json:"purchasable"`
}
func (pr *Program) checkInventory() {
url := fmt.Sprintf("https://www.bestbuy.ca/ecomm-api/availability/products?accept-language=en-CA&skus=%s", pr.Sku)
log.Infof("URL %s", url)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
log.Info(string(bodyBytes))
var inv Inventory
json.Unmarshal(bodyBytes, &inv)
log.Infof("%+v", inv)
}

控制台:

INFO[2020-04-07T03:01:10-07:00] URL https://www.bestbuy.ca/ecomm-api/availability/products?accept-language=en-CA&skus=12341231 
INFO[2020-04-07T03:01:10-07:00] {"availabilities":[{"pickup":{"status":"OnlineOnly","purchasable":false},"shipping":{"status":"InStockOnlineOnly","purchasable":true},"sku":"12341231
,"sellerId":"438178","saleChannelExclusivity":"OnlineOnly","scheduledDelivery":false,"isGiftCard":false,"isService":false}]}
INFO[2020-04-07T03:01:10-07:00] {Availabilities:[]}

问题出在json.Unmarshall调用中。返回错误:"invalid character 'ï' looking for beginning of value” from json.Unmarshal

正如这里所解释的:服务器将向您发送一个带有字节顺序标记(BOM(的UTF-8文本字符串。BOM表标识文本是UTF-8编码的,但在解码之前应将其删除。

这可以通过以下行完成(使用包"字节"(:

body = bytes.TrimPrefix(body, []byte("xefxbbxbf"))

因此产生的工作代码是:

// Inventory ...
type Inventory struct {
Availabilities []Availability `json:"availabilities"`
}
// Availability ...
type Availability struct {
Sku                    string   `json:"sku"`
SellerID               string   `json:"sellerId"`
SaleChannelExclusivity string   `json:"saleChannelExclusivity"`
ScheduledDelivery      bool     `json:"scheduledDelivery"`
IsGiftCard             bool     `json:"isGiftCard"`
IsService              bool     `json:"isService"`
Pickup                 Statuses `json:"pickup"`
Shipping               Statuses `json:"shipping"`
}
// Statuses ..
type Statuses struct {
Status      string `json:"status"`
Purchasable bool   `json:"purchasable"`
}
func (pr *Program) checkInventory() {
url := fmt.Sprintf("https://www.bestbuy.ca/ecomm-api/availability/products?accept-language=en-CA&skus=%s", pr.Sku)
log.Infof("URL %s", url)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
body := bytes.TrimPrefix(bodyBytes, []byte("xefxbbxbf"))
log.Info(string(body))
var inv Inventory
err = json.Unmarshal([]byte(body), &inv)
if err != nil {
log.Fatal(err)
}
log.Infof("%+v", inv)
}

最新更新