使用Golang正确解码复杂的JSON数据

huangapple go评论97阅读模式
英文:

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=&quot;&lt;name&gt;&quot;

谢谢。

英文:

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:

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

My Go structs are:

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

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

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

type _Volume struct {
	Current float64 `json:&quot;current&quot;`
	All     float64 `json:&quot;all&quot;`
	Perc    float64 `json:&quot;perc&quot;`
}

type _Markets struct {
	Btce     _BtceInfo     `json:&quot;btce&quot;`				
	Coinbase _CoinbaseInfo `json:&quot;coinbase&quot;`			
}

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

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

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

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

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

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

type _WebSocket struct {
	Wsurl  string `json:&quot;ws_url&quot;`
	WssUrl string `json:&quot;wss_url&quot;`
}

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:&quot;&lt;name&gt;&quot; not json=&quot;&lt;name&gt;&quot;

Thanks.

答案1

得分: 3

你所有的 JSON 标签都需要使用 json:"<name>",而不是 json="<name>"

英文:

All your json tags need to be json:&quot;&lt;name&gt;&quot; not json=&quot;&lt;name&gt;&quot;

huangapple
  • 本文由 发表于 2016年11月5日 12:56:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/40434913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定