json - 无法解码golang回应

标签 json go marshalling unmarshalling

我一直在尝试通过将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
正如here所解释的:服务器正在向您发送带有字节顺序标记(BOM)的UTF-8文本字符串。 BOM标识文本是UTF-8编码的,但应在解码之前将其删除。

可以使用以下行(使用包“bytes”)完成此操作:

body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf"))

因此,最终的工作代码为:
// 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("\xef\xbb\xbf"))
    log.Info(string(body))

    var inv Inventory
    err = json.Unmarshal([]byte(body), &inv)
    if err != nil {
        log.Fatal(err)
    }
    log.Infof("%+v", inv)
}

关于json - 无法解码golang回应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61077483/

相关文章:

python - 基于平台分割数据(来自文本/json文件)(使用Python)

go - 你如何确定当前运行的可执行文件的完整路径?

pointers - 如何共享嵌入的结构指针

json - 有没有办法通过 CloudKit 仪表板导入批量数据?

Javascript:如何直接从对象生成格式化的易于阅读的 JSON?

Javascript JSON.parse with\u0000

go - 我应该使用 Get 方法获取值还是应该直接使用字段?

GWT-RPC 和不可变传输对象

windows - 为 COM 接口(interface)启用编码(marshal)处理需要什么?

python - 如何使用 marshalled python 编写 p4 更改更改列表创建脚本