英文:
Decoding a complex JSON correctly using Golang
问题
我正在尝试从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.596730075e+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 }
编辑1:
抱歉,输出可能会有变化,因为我直接从URL(http://btc.blockr.io/api/v1/coin/info)解析。
编辑2:
感谢@John S Perayil发现了错误。
我的JSON标签应该是json:"<name>"
而不是json="<name>"
。
谢谢。
英文:
I'm trying to parse a JSON from a URL. But I'm facing a problem while decoding values. Some are correct and the others are nil (empty strings or nil values for float64).
The JSON fetched from the URL is like this:
{
"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":""
}
My Go structs are:
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"`
}
My incorrect output is:
{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:
Sorry the ouput my change because i'm parsing directly from the URL (http://btc.blockr.io/api/v1/coin/info)
Edit2:
Thanks to @John S Perayil who found the mistake.
My JSON Tags need to be json:"<name>"
not json="<name>"
Thanks.
答案1
得分: 3
你所有的 JSON 标签都需要使用 json:"<name>"
,而不是 json="<name>"
。
英文:
All your json tags need to be json:"<name>"
not json="<name>"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论