Невозможно отменить ответ golang - PullRequest
0 голосов
/ 07 апреля 2020

Я пытался извлечь некоторую 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:[]}

1 Ответ

3 голосов
/ 07 апреля 2020

Проблема в вызове json.Unmarshall. Он возвращает ошибку: "invalid character 'ï' looking for beginning of value” from json.Unmarshal

Как объясняется здесь : Сервер отправляет вам текстовую строку UTF-8 с меткой порядка байтов (BOM). Спецификация определяет, что текст имеет кодировку UTF-8, но его следует удалить перед декодированием.

Это можно сделать с помощью следующей строки (с использованием пакета "байты"):

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)
}
...