Go - Python等效的嵌套数据结构 [英] Nested data structures in Go - Python equivalent
问题描述
我可以很容易地在Python中创建这个数据结构:
data = {'Locations':[],
'Dates':[],
'Properties':[{'key':'data1','value':'data2'}],
'Category':'all'}
然后可以轻松地将它们编组为Python中的JSON。例如
print json.dumps(data)
pre>
{Category:all,日期:[],位置:[],属性:[{value:data2,key:data1}]}
然而,我正在试图创建相同的结构,然后在Go中将其转换为JSON。 Go看起来非常有希望,并且正是我创建跨平台应用程序所需要的,但是这个东西似乎让人感到非常困难。
这是我试过的,但是我无法得到结构来包含应该包围属性元素的方括号。
import(
fmt
encoding / json
)
func main {
data:= map [string] interface {} {Offset:0,Properties:map [string] string {value:data2,key: data1},Category:all,Locations:[] string {},Dates:[] string {}}
data_json,_:= json.Marshal(data)
fmt.Println(string(data_json))
}
p>
{Category:all,Dates:[],Locations:[],Offset 0,Properties:{key:data1,value:data2}}
继承人演示: http://play.golang.org/p/49Kytg6v_C
解决方案虽然OneOfOne的解决方案在字面意义上是有效的,
// json:stuff会在读取时使用该json标签或写json。
类型属性struct {
密钥字符串`json:key`
Val字符串`json:value`
}
//, omitempty允许它在编写json时忽略空字符串,
//我把它放在这里,因为你的json例子中没有一个Offset字段
//你可以在其他字段中做这个。
类型MyType结构{
偏移字符串`json:,omitempty`
属性[]属性
类别字符串
位置[]字符串
日期[ ] string
}
当然,您也可以考虑使用内置或自定义的Go这些字段中的某些字段的类型,例如对日期字段使用
[] time.Time
。这使得读取/写入任意json特别困难或即时读取变得更加困难,但由于您需要某个逻辑来解释这些字段,因此在Go中,通常将其视为一个结构
要阅读/放出json,您可以这样做:
importencoding / json
// ...
stuff:= MyType {}
json.Unmarshal(myJsonData,&stuff)
//东西现在是{[{data1 data2}]所有[] []}
出,_:= json.Marshal(东西)
//字符串(出)现在是{属性:[ key:data1,value:data2}],
//Category:all,Locations:[],Dates:[]}
游乐场: http://play.golang.org/p/jIHgXmY13R
I can create this data structure in Python really easily:
data = {'Locations': [], 'Dates': [], 'Properties': [{'key': 'data1', 'value': 'data2'}], 'Category': 'all'}
Which can then be marshalled to JSON in Python just as easily. e.g.
print json.dumps(data) {"Category": "all", "Dates": [], "Locations": [], "Properties": [{"value": "data2", "key": "data1"}]}
However, I'm tearing my hair out trying to create the same structure then convert it to JSON in Go. Go looks to be very promising and just what I need for creating cross platform applications, but boy this stuff seems to be frustratingly difficult.
This is what I've tried, however I cant get the structure to include the square brackets that should surround the properties element.
import ( "fmt" "encoding/json" ) func main() { data := map[string]interface{}{"Offset": "0", "Properties": map[string]string{"value": "data2", "key": "data1"}, "Category": "all", "Locations": []string{}, "Dates": []string{} } data_json, _ := json.Marshal(data) fmt.Println(string(data_json)) }
Which outputs:
{"Category":"all","Dates":[],"Locations":[],"Offset":"0","Properties":{"key":"data1","value":"data2"}}
Heres a demo: http://play.golang.org/p/49Kytg6v_C
解决方案While OneOfOne's solution works in a literal sense, with Go's static typing you likely want a struct.
// The `json:"stuff"` will cause it to use that json tag when reading or writing json. type Property struct { Key string `json:"key"` Val string `json:"value"` } // ,omitempty allows it to ignore the empty string "" when writing json, // I put this here because one of your json examples had no Offset field // you can do this with other fields too. type MyType struct { Offset string `json:",omitempty"` Properties []Property Category string Locations []string Dates []string }
Of course, you could also consider using built-in or custom Go types for some of those fields, such as using a
[]time.Time
for the Dates field. This makes it more difficult to just read/write arbitrary json ad-hoc or on the fly, but since you need to have some logic somewhere to interpret those fields, in Go it generally makes much more sense to treat it as a struct most of the time.To read in/put out the json, you would then do
import "encoding/json" //... stuff := MyType{} json.Unmarshal(myJsonData, &stuff) // stuff is now { [{data1 data2}] all [] []} out,_ := json.Marshal(stuff) // string(out) is now {"Properties":[{"key":"data1","value":"data2"}], // "Category":"all","Locations":[],"Dates":[]}
Playground: http://play.golang.org/p/jIHgXmY13R
这篇关于Go - Python等效的嵌套数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!