dongliugu8843 2017-02-19 18:23
浏览 29

取消对JSON输入的意外结束,同时对结构进行了编组

Could you help me to understand why I'm always getting the error unexpected end of JSON input while trying to unmarshal the following json to the LimitOrder struct?

P.S.: if I use map[string]json.RawMessage instead of LimitOrder struct I'm able to execute the unmarshal.

{
  "response_data": {
    "order": {
      "order_id": 3,
      "coin_pair": "BRLBTC",
      "order_type": 1,
      "status": 4,
      "has_fills": true,
      "quantity": "1.00000000",
      "limit_price": "900.00000",
      "executed_quantity": "1.00000000",
      "executed_price_avg": "900.00000",
      "fee": "0.00300000",
      "created_timestamp": "1453835329",
      "updated_timestamp": "1453835329",
      "operations": [
        {
          "operation_id": 1,
          "quantity": "1.00000000",
          "price": "900.00000",
          "fee_rate": "0.30",
          "executed_timestamp": "1453835329"
        }
      ]
    }
  },
  "status_code": 100,
  "server_unix_timestamp": "1453835329"
}

LimitOrder struct

type LimitOrder struct {
  OrderId int `json:"order_id"`
  CoinPair string `json:"coin_pair"`
  OrderType int `json:"order_type"`
  Status int `json:"status"`
  HasFills bool `json:"has_fills"`
  Quantity float64 `json:"quantity,string"`
  LimitPrice float64 `json:"limit_price,string"`
  ExecutedQuantity float64 `json:"executed_quantity,string"`
  ExecutedPriceAvg float64 `json:"executed_price_avg,string"`
  Fee float64 `json:"fee,string"`
  Operations []*Operation `json:"operations"`
  CreatedTimestamp string `json:"created_timestamp"`
  UpdatedTimestamp string `json:"updated_timestamp"`
}

and this is how I'm trying to unmarshal it

func (limitOrder *LimitOrder) UnmarshalJSON(buf []byte) error {

  tmp := make(map[string]json.RawMessage)
  if err := json.Unmarshal(buf, &tmp); err != nil {
    return err
  }

  tmp2 := make(map[string]json.RawMessage)

  if err := json.Unmarshal(tmp["response_data"], &tmp2); err != nil {
    return err
  }

  if err := json.Unmarshal(tmp2["order"], limitOrder); err != nil {
    return err
  }

  return nil
}
  • 写回答

1条回答 默认 最新

  • dongshi9719 2017-03-03 17:02
    关注

    I found some help on golang-nuts group. You can check the answer here https://groups.google.com/forum/#!topic/golang-nuts/SZXBcXgUoo0. To make a long story short, the problem was with my structures, I've built structures only for a small piece of the whole json, so I fixed it building structures for the whole json response.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题