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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动