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条)

报告相同问题?

悬赏问题

  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要