2016-11-05 5 views
0

URLからJSONを解析しようとしています。しかし、値をデコードする際に問題に直面しています。いくつかは正しく、他のものはnil(空の文字列またはfloat64のnil値)です。Golangを使用して複雑なJSONを正しくデコードする

URLからフェッチJSONは、このようなものです:

{ 
    "status":"success", 
    "data":{ 
     "coin":{ 
      "name":"Bitcoin", 
      "abbr":"BTC", 
      "logo":"", 
      "homepage":"" 
     }, 
     "volume":{ 
      "current":15967300, 
      "all":21000000, 
      "perc":76.03 
     }, 
     "markets":{ 
      "btce":{ 
       "name":"BTC-e", 
       "last_update_utc":"2016-11-05T01:25:02Z", 
       "value":694.696, 
       "currency":"USD", 
       "daily_change":{ 
        "value":"699.05600000", 
        "perc":-0.62, 
        "diff":-4.36 
       } 
      }, 
      "coinbase":{ 
       "name":"Coinbase", 
       "last_update_utc":"2016-11-05T01:25:02Z", 
       "value":705.65, 
       "currency":"USD", 
       "daily_change":{ 
        "value":"700.87000000", 
        "perc":0.68000000000001, 
        "diff":4.78 
       } 
      } 
     }, 
     "last_block":{ 
      "nb":437388, 
      "time_utc":"2016-11-05T01:27:32Z", 
      "nb_txs":570, 
      "fee":"0.06545766", 
      "difficulty":"253618246641.490000000000000" 
     }, 
     "next_difficulty":{ 
      "difficulty":255675205724.12, 
      "retarget_in":84, 
      "retarget_block":437472, 
      "perc":0.81104538410295 
     }, 
     "websocket":{ 
      "ws_url":"ws:\/\/btc.blockr.io:9081", 
      "wss_url":"wss:\/\/btc.blockr.io:8081" 
     } 
    }, 
    "code":200, 
    "message":"" 
} 

マイゴーの構造体

は次のとおりです。

type ResponseInfo struct { 
    Status string `json:"status"` 
    Data Info `json:"data"`      
    Code float64 `json:"code"` 
    Message string `json:"message,omitempty"` 
} 

type Info struct { 
    Coin  _Coin  `json:"coin"`     
    Volume _Volume `json:"volume"`     
    Markets _Markets `json:"markets"`    
    LastBlock _LastBlock `json:"last_block"`    
    NextDiff _NextDiff `json:"next_difficulty"`  
    WebSocket _WebSocket `json:"websocket"`    
} 

type _Coin struct { 
    Name  string `json:"name"` 
    Abbr  string `json:"abbr"` 
    Logo  string `json:"logo"` 
    HomePage string `json:"homepage,omitempty"` 
} 

type _Volume struct { 
    Current float64 `json:"current"` 
    All  float64 `json:"all"` 
    Perc float64 `json:"perc"` 
} 

type _Markets struct { 
    Btce  _BtceInfo  `json:"btce"`     
    Coinbase _CoinbaseInfo `json:"coinbase"`    
} 

type _BtceInfo struct { 
    Name  string  `json:"name"` 
    LastUpdate string  `json:"last_update_utc"` 
    Value  float64 `json:"value"` 
    Currency string  `json:"currency"` 
    DailyChange _BtceDaily `json:"daily_change,omitempty"`  
} 

type _BtceDaily struct { 
    Value string `json:"value"` 
    Prec float64 `json:"prec"` 
    Diff float64 `json:"diff"` 
} 

type _CoinbaseInfo struct { 
    Name  string   `json:"name"` 
    LastUpdate string   `json:"last_update_utc"` 
    Value  float64  `json:"value"` 
    Currency string   `json:"currency"` 
    DailyChange _CoinbaseDaily `json:"daily_change"` 
} 

type _CoinbaseDaily struct { 
    Value string `json:"value"` 
    Prec float64 `json:"prec"` 
    Diff float64 `json:"diff"` 
} 

type _LastBlock struct { 
    Nb   float64 `json:"nb"` 
    Time  string `json:"time_utc"` 
    NbTxs  float64 `json:"nb_txs"` 
    Fee  string `json:"fee"` 
    Difficulty string `json:"difficulty"` 
} 

type _NextDiff struct { 
    Difficulty float64 `json:"difficulty"` 
    RetargetIn float64 `json:"retarget_in"` 
    RetargetBlock float64 `json:"retarget_block"` 
    Perc   float64 `json:"perc"` 
} 

type _WebSocket struct { 
    Wsurl string `json:"ws_url"` 
    WssUrl string `json:"wss_url"` 
} 

私の誤った出力は次のとおりです。

{success {{Bitcoin BTC } {1.59675375e+07 2.1e+07 76.04} {{BTC-e 694 USD { 0 0}} {Coinbase 704.2 USD { 0 0}}} {0 0 } {0 0 0 0} { }} 200 } 

EDIT1: 申し訳ありませんURLから直接解析しているので私の変更を外してください(http://btc.blockr.io/api/v1/coin/info

Edit2: @John S Perayilに間違いを見つけたことに感謝します。 My JSONのタグはjson:"<name>"でない必要がありますjson="<name>"

ありがとうございました。

答えて

3

あなたのすべてのJSONタグはjson:"<name>"ないjson="<name>"

+1

する必要がありますはい!あなたが正しい。それは期待どおりに動作します。 すみません、私は現在、勉強しようとしています。再度、感謝します。 –

関連する問題