GoLang:如何将地图嵌入到一个结构体中,使其具有平面的json表示? [英] GoLang: How to embed a map into a struct so that it has a flat json representation?
问题描述
为了创建一个类似表的结构,我以前的应用程序以下列格式序列化了我的行数据:
{key1:... ,key2:...,15/04:1.3,15/05:1.2,....17/08:0.8}
In order to create a table-like structure, I serialized my row data in following format in my previous application:
{ "key1": "...", "key2": "...", "15/04": 1.3, "15/05": 1.2, .... "17/08": 0.8 }
现在,我正在Go中重写它,以便通过实践经验学习语言。在Go中,可以通过将它们嵌入到另一个结构体中来组合两个结构体。该结构体中的编组json将具有平面结构,即生成的json对象将具有无嵌套的第一和第二结构体的字段的并集。以下是一个示例: https://play.golang.org/p/jbJykip7pw (from < a href =http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/ =nofollow> http://attilaolah.eu/2014/09/10 / json-and-struct-composition-in-go / )
Now I am trying to rewrite it in Go in order to learn the language with hands-on experience. In Go, one can compose two structs together by embedding them into another struct. The marshalled json out of that struct will have a flat structure, i.e. the resulting json object will have union of fields of first and second structs without nesting. Here is an example: https://play.golang.org/p/jbJykip7pw (from http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/)
我猜想我也可以将地图嵌入一个结构体,以便我可以在上面编组json使用以下类型定义:
I guessed I could also embed a map into a struct so that I can marshall above json using following type definitions:
type Row struct {
key1 string
key2 string
RowData
}
type RowData map[string]float64
...
func main() {
row := Row{
"...",
"...",
RowData{
"15/04": 1.3, "15/05": 1.2, .... "17/08": 0.8,
},
}
}
但是,我在Row对象中创建了一个字段RowData,而不是将RowData中的条目附加到我想要的平面json对象中:
{ke y1:...,key2:...,RowData:{15/04:1.3,15/05:1.2,....17/08 0.8}}
But this created a field 'RowData' field in my 'Row' object, instead of appending entries in the RowData into my desired flat json object:
{ "key1": "...", "key2": "...", "RowData": { "15/04": 1.3, "15/05": 1.2, .... "17/08": 0.8 } }
我想知道,如果有一种方法将地图或切片嵌入到结构体中,结果json对象是平的,没有在类型行上定义
? MarshalJSON
函数
I would like to know, if there is a way to embed maps or slices into a struct so that resulting json object is flat, without defining a MarshalJSON
function on type Row
?
推荐答案
简短的答案是否定的。该语言不允许您将任何类型(切片或地图)嵌入到结构体中。
The short answer is no. The language does not allow you to embed either type (slice or map) in a struct.
只需使用 map [string] interface {}
。处理key1和key2的值是字符串,其他所有内容都是浮点数的事实。这真的是你获得该输出的唯一方法。你可以使这个问题像你想要的那样复杂(像转换成一个类似于你的东西或类似的东西),但是如果你厌恶实现 MarshalJSON
唯一的将产生您想要的结果的模型是 map [string] interface {}
Just use a map[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 implementing MarshalJSON
the only model which will produce the results you want is map[string]interface{}
这篇关于GoLang:如何将地图嵌入到一个结构体中,使其具有平面的json表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!