dongzhi4239 2017-09-19 08:24
浏览 48
已采纳

如何使用goreq接收复杂的json?

I'm a beginner in Go and I'm trying to call a json rest-API for which I'm trying to use the goreq request lib. In the readme it gives the following example for decoding the received json:

type Item struct {
    Id int
    Name string
}

var item Item

res.Body.FromJsonTo(&item)

I understand this example, but the json I'm receiving is way more complex (see below). I'm not sure how I would create a struct which represents this complex structure. Can I write it in one struct, or do I need to use maps for the bid and ask arrays, another struct for the bid and ask objects, and yet one more struct for the "vars" object?

Isn't there any automagic json_to_struct() function which dynamically builds the struct upon receiving the json (I've looked but I can't find anything)?

All tips are welcome!

{
    "success": true,
    "message": "something",
    "vars": {"some": "value", "thenumber": 7612.32},
    "result": {
        "bids": [
            {"quantity": 2, "price": 416.02, "cm": "some text"},
            etc..
        ],
        "asks": [
            {"quantity": 1, "price": 420.02, "cm": "some text"},
            etc..
        ],
        "slip": 4
    }
}
  • 写回答

1条回答 默认 最新

  • douci4026 2017-09-19 08:57
    关注

    TL;DR

    Can I write it in one struct

    Yes

    or do I need to use maps for the bid and ask arrays, and yet another struct for the bid and ask objects?

    Yes

    Before I actually do the hand holding here, I'm gonna recommend that you actually spend some more time getting used to the way things are done in Go. This might be tedious work but it is very simple.

    Long Version

    I'm not sure how I would create a struct which represents this complex structure.

    You are going to do it the same way if you just had to represent this in your program, without any JSON.

    Let's explain what you have in that message in English before we translate it to Go:

    You have a struct with the fields:

    • Success - Boolean
    • Message - String
    • Result - Struct

    Now, Result is also a struct, so we need to describe it:

    • Bids - Array of Struct
    • Asks - Array of Struct
    • Slip - Integer

    Let's do the same for Bids and Asks (the look the same so I'll save space)

    • Quantity - Integer
    • Price - Float64
    • Cm - String

    Now let's define our struct:

    Let's call the container struct "Response" and the result one "Result"

    type Response struct {
      Success bool `json:"success"`
      Message string `json:"message"`
      Result  Result `json:"result"`
    }
    
    type Result struct {
      Bids []Quote `json:"bids"`
      Asks []Quote `json:"asks"`
      Slip int `json:"slip"`
    }
    
    type Quote struct { 
    //As asks and bids are have the same structure we can save code
      Quantity int `json:"quantity"`
      Price float64 `json:"price"`
      Cm string `json:"cm"`
    }
    

    Don't forget to add annotations so that the (Un)Marshaling functions can properly match fields in JSON to the Struct.

    I believe the above is the best and more maintainable way of implementing this, however you can also write the same as one struct with nested anonymous structs:

    type Response struct {
        Success bool   `json:"success"`
        Message string `json:"message"`
        Result  struct {
            Bids []struct {
                Quantity int     `json:"quantity"`
                Price    float64 `json:"price"`
                Cm       string  `json:"cm"`
            } `json:"bids"`
            Asks []struct {
                Quantity int     `json:"quantity"`
                Price    float64 `json:"price"`
                Cm       string  `json:"cm"`
            } `json:"asks"`
            Slip int `json:"slip"`
        } `json:"result"`
    }
    

    By the way, there's an interesting tool that I found a few weeks ago that automatically transforms JSON to a Go struct that you can declare in your code right away: https://transform.now.sh/json-to-go/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改