Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
golang的Json编码_Json_Go - Fatal编程技术网

golang的Json编码

golang的Json编码,json,go,Json,Go,这是我从网站编码json文件的代码,如果它编码多个产品,它就可以工作 resp, err := http.Get(url + t.Keywords[0] + ".json") if err != nil { fmt.Println("error while getting productsRaw") } productsRaw, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("error while pa

这是我从网站编码json文件的代码,如果它编码多个产品,它就可以工作

resp, err := http.Get(url + t.Keywords[0] + ".json")
if err != nil {
    fmt.Println("error while getting productsRaw")
}
productsRaw, err := ioutil.ReadAll(resp.Body)
if err != nil {
    fmt.Println("error while parsing body")
}
productsRawString := string(productsRaw)
productsData := ShopifyProducts{}
json.Unmarshal([]byte(productsRawString), &productsData)
fmt.Println(productsData.Products)\
例如,这是Println(productsRawString)显示的内容,所以url可以工作,但是json.Unmarshal([]字节(productsRawString),&productsData)不工作

{"product":{"id":4420737073222,"title":"W AIR MAX VERONA","body_html":"\u003cp\u003e\u003cspan\u003eDesigned with every woman in mind, the mixed-material upper features a plush collar, flashy colours and unique stitching patterns. Nike Air cushioning combines with the lifted foam heel for a modern touch, adding comfort and style to your journey.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e \u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e-Smooth 
商品结构


解组有什么问题?

正如我在您的
ShopifyProducts
结构中看到的那样,您已将
产品声明为
数组。但是在序列化字符串
中,product
是一个
对象
。所以它无法解组原始字符串

您还声明,在产品不止一个的情况下,它是有效的。在您的结构中,它的工作产品是一个数组

可能的解决办法:

  • 预处理原始字符串,并在产品还不是数组时将其绑定到数组中。通过这种方式,您将能够以一种常见的方式对其进行解组。但这将是一个解决方案,您需要编写不必要的预处理代码
  • 只需更改从何处获取的数据。始终将产品保存为数组,这样您就可以从端点调用GET,并且它始终具有通用格式

正如我在您的
ShopifyProducts
结构中所看到的,您已将
产品声明为
数组。但是在序列化字符串
中,product
是一个
对象
。所以它无法解组原始字符串

您还声明,在产品不止一个的情况下,它是有效的。在您的结构中,它的工作产品是一个数组

可能的解决办法:

  • 预处理原始字符串,并在产品还不是数组时将其绑定到数组中。通过这种方式,您将能够以一种常见的方式对其进行解组。但这将是一个解决方案,您需要编写不必要的预处理代码
  • 只需更改从何处获取的数据。始终将产品保存为数组,这样您就可以从端点调用GET,并且它始终具有通用格式

Unmarshal
返回未捕获的错误。执行此操作&检查错误以提示根本原因。我遇到这个错误API是否返回产品列表?您的
ShopifyProducts
结构需要一个
Product
列表,但json只有一个对象(而不是列表)。@LTUniger您提供的json不完整,则您提供的结构类型不完整。如果您正在寻求帮助,则需要首先提供可实际用于重新创建问题的代码。
Unmarshal
返回您未捕获的错误。执行此操作&检查错误以提示根本原因。我遇到这个错误API是否返回产品列表?您的
ShopifyProducts
结构需要一个
Product
列表,但json只有一个对象(而不是列表)。@LTUniger您提供的json不完整,则您提供的结构类型不完整。如果您正在寻求帮助,那么首先需要提供可用于重新创建问题的代码。
type ShopifyProducts struct {
Products []struct {
    BodyHTML  string `json:"body_html"`
    CreatedAt string `json:"created_at"`
    Handle    string `json:"handle"`
    ID        int    `json:"id"`
    Images    []struct {
        CreatedAt  string        `json:"created_at"`
        Height     int           `json:"height"`
        ID         int           `json:"id"`
        Position   int           `json:"position"`
        ProductID  int           `json:"product_id"`
        Src        string        `json:"src"`
        UpdatedAt  string        `json:"updated_at"`
        VariantIds []interface{} `json:"variant_ids"`
        Width      int           `json:"width"`
    } `json:"images"`