JSON输入的意外终端,同时向结构进行核算

  • 本文关键字:结构 意外 终端 JSON go
  • 更新时间 :
  • 英文 :


您能帮我了解为什么我总是在尝试将以下JSON汇总到LimitOrder struct时始终遇到错误unexpected end of JSON input

P.S。:如果我使用map[string]json.RawMessage而不是LimitOrder struct,我可以执行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"`
}

这就是我试图解散它的方式

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
}

我在Golang-Nuts组上找到了一些帮助。您可以在此处查看答案https://groups.google.com/forum/# !! topic/golang-nuts/szxbcxguoo0。简而言之,问题出在我的结构上,我只为整个JSON的一小部分建造了结构,因此我修复了整个JSON响应的IT构建结构。

最新更新