dqq3623 2019-03-14 00:27
浏览 19
已采纳

如何使json响应正常运行

The code below works fine by displaying the json array data. Here is the working Json response in the code below

 {"provision":"provision section 1",
    "subsets": [{"item":"milk"},{"payments": [{"price": "200 usd"}]}]}

Now I have New json response as follow. In the new json response below the parameter subsets is now surrounded by brackets {}

{
"provision":{"subsets": [{"item":"milk"},{"payments": [{"price": "200 usd"}]}]}
}

If I integrate the New json in the code it shows error cannot unmarshal object into Go struct field Head.Provision. Any solutions on resolving the object issue will be appreciated

Here is the code

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
)

type Head struct {
    Provision  string `json:"provision"`
    Subsets []Subset `json:"subsets"`

}

type Subset struct {
    Payments []Payment `json:"payments"`
        Item string `json:"item"`
}

type Payment struct {
    Price string `json:"price"`
}

func main() {
/*
// old working json
    m := []byte(`

        {"provision":"provision section 1",
       "subsets": [{"item":"milk"},{"payments": [{"price": "200 usd"}]}]}

`)
*/

// New json response not working

m := []byte(`
    {
"provision":{"subsets": [{"item":"milk"},{"payments": [{"price": "200 usd"}]}]}
}  

`)

    r := bytes.NewReader(m)
    decoder := json.NewDecoder(r)

    val := &Head{}
    err := decoder.Decode(val)
    if err != nil {
        log.Fatal(err)
    }

fmt.Println(val.Provision)


    // Subsets is a slice so you must loop over it 
    for _, s := range val.Subsets {
        fmt.Println(s.Item)
        // within Subsets, payment is also a slice
        // then you can access each price
        for _, a := range s.Payments {
            fmt.Println(a.Price)
        }
    }


}
  • 写回答

2条回答 默认 最新

  • duanji9481 2019-03-14 01:49
    关注

    Below is how I got it working. Thanks

    package main
    
    import (
        "bytes"
        "encoding/json"
        "fmt"
        "log"
    )
    
    type Head struct {
        Provision  Prov `json:"provision"`
        //Subsets []Subset `json:"subsets"`
    
    }
    
    
    
    type Prov struct {
       Subsets []Subset `json:"subsets"`
    }
    
    
    
    
    
    type Subset struct {
        Payments []Payment `json:"payments"`
            Item string `json:"item"`
    }
    
    type Payment struct {
        Price string `json:"price"`
    }
    
    func main() {
    
    m := []byte(`
    
            {"provision":
           {"subsets": [{"item":"milk"},{"payments": [{"price": "200 usd"}]},
    {"item":"SUGAR"},{"payments": [{"price": "600 usd"}]}
    ]}
    }
    
    `)
    
    
        r := bytes.NewReader(m)
        decoder := json.NewDecoder(r)
    
        val := &Head{}
        err := decoder.Decode(val)
        if err != nil {
            log.Fatal(err)
        }
    
    //fmt.Println(val.Provision)
    
    
        // Subsets is a slice so you must loop over it 
        for _, s := range val.Provision.Subsets {
            fmt.Println(s.Item)
            // within Subsets, payment is also a slice
            // then you can access each price
            for _, a := range s.Payments {
                fmt.Println(a.Price)
            }
        }
    
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案