donglv9813 2018-01-30 16:26
浏览 55
已采纳

如何将请求正文解码/解组为Golang结构和结构切片?

How do I properly unmarshal this in Golang?

{
    "symbol": "ZVZZT.O",
    "params": [{
        "forward": 0,
        "period": 3,
        "ref": "high",
        "indicator": "sma",
        "freq": "day"
    }, {
        "forward": 1,
        "period": 8,
        "ref": "close",
        "indicator": "ema",
        "freq": "week"
    }]
}

into these structs

type Iteration4RequestBody struct {
    Symbol string             `json:"symbol"`
    Params []Iteration4Params `json:"params"`
}

type Iteration4Params struct {
    Forward   int    `json:"forward"`
    Period    int    `json:"period"`
    Ref       string `json:"ref"`
    Indicator string `json:"indicator"`
    Freq      string `json:"freq"`
}

I need to be able to use []Iteration4Params, but right now, this is empty when I decode it

EDIT 2 (NEW CODE I USED)

body, err := ioutil.ReadAll(r.Body)
if err != nil {
    panic(err)
}
var t Iteration4RequestBody
err = json.Unmarshal(body, &t)
if err != nil {
    panic(err)
}
fmt.Println(t)

Result is {ZVZZT.O []}

Edit 3: Works now. Can't remember the last thing I did before it worked though. Never mind. Thanks for all your answers by the way.

  • 写回答

2条回答 默认 最新

  • dongwei9365 2018-01-30 16:40
    关注

    I'm confused about a few things you're doing:

    • using a for when you're decoding a request.Body
    • expecting more than one element
    • not just appending b directly

    Dropping the loop and using either json.Unmarshal or json.NewDecoder works fine. I could keep the for loop, and it would still work. You should show the rest of your code, you have a problem elsewhere, either:

    • the request body is empty or already read and closed
    • you're returning the parsed objects in a strange way
    • something else?

    Here's working code (without the for loop), showing both possibilities:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "io"
        "strings"
    )
    
    var j = `
    {
        "symbol": "ZVZZT.O",
        "params": [{
            "forward": 0,
            "period": 3,
            "ref": "high",
            "indicator": "sma",
            "freq": "day"
        }, {
            "forward": 1,
            "period": 8,
            "ref": "close",
            "indicator": "ema",
            "freq": "week"
        }]
    }`
    
    type Iteration4RequestBody struct {
        Symbol string             `json:"symbol"`
        Params []Iteration4Params `json:"params"`
    }
    
    type Iteration4Params struct {
        Forward   int    `json:"forward"`
        Period    int    `json:"period"`
        Ref       string `json:"ref"`
        Indicator string `json:"indicator"`
        Freq      string `json:"freq"`
    }
    
    func main() {
        // Using json.Unmarshal
        var it Iteration4RequestBody
        err := json.Unmarshal([]byte(j), &it)
        if err != nil {
            panic(err)
        }
        fmt.Println(it)
    
        // Using a json.Decoder
        var it2 Iteration4RequestBody
        dec := json.NewDecoder(strings.NewReader(j))
        if err := dec.Decode(&it2); err != nil && err != io.EOF {
            panic(err)
        }
        fmt.Println(it2)
    }
    

    Outputs:

    {ZVZZT.O [{0 3 high sma day} {1 8 close ema week}]}
    {ZVZZT.O [{0 3 high sma day} {1 8 close ema week}]}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制