2017-02-19 113 views
0

你能帮我理解,为什么我总是得到错误unexpected end of JSON input而试图将下面的json解组到LimitOrder结构?JSON输入的意外结束,而解组结构

P.S .:如果我使用map[string]json.RawMessage而不是LimitOrder结构我能够执行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结构

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 
} 
+0

我找不到任何错误。除此之外,如果我将它运行在操场上[https://play.golang.org/p/udPQ_TayXG](https://play.golang.org/p/udPQ_TayXG) –

回答