map[string]interface{}
をmarshal,unmarshalできる
必須パラメータを埋め込みにしてみた
オプションのキーをフラットにしたい
上の例では、 option
のキーにオプションのパラメータを詰めたことで option: map[string]interface{}
にmarshalできた。
以下のようなJSONはmarshalできるか?
The short answer is no. The language does not allow you to embed either type (slice or map) in a struct.
Just use amap[string]interface{}
. Deal with the fact that the values for “key1” and “key2” are strings and everything else is a float somewhere else. That’s really the only way you’re getting that output. You can make the problem as complicated as you’d like beyond this (like transform into a type more like yours or something) but if you’re averse to implementingMarshalJSON
the only model which will produce the results you want ismap[string]interface{}
その場合は単に map[string]interface{}
にするくらい