我试图从URL解析JSON。但我在解码数值时遇到问题。有些是正确的,其他是nil(float64的空字符串或nil值)。
从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":""
}
我的Go结构是:
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: 抱歉,我输出了我的更改,因为我直接从网址解析(http://btc.blockr.io/api/v1/coin/info)
EDIT2:
感谢@John S Perayil发现了这个错误。
我的JSON代码需要json:"<name>"
而不是json="<name>"
感谢。
答案 0 :(得分:3)
您的所有json标记都必须为json:"<name>"
而不是json="<name>"