英文:
Converting json to slice of maps and slice of maps to json in Go without using structs
问题
我正在尝试将来自HTTP请求的JSON字符串转换为映射切片。同时,我还需要将映射切片转换为JSON字符串,以便用于HTTP响应。
我想要在以下两种情况下将其转换为映射切片:
-
来自HTTP请求的JSON字符串可能是多个具有相同键值的JSON对象的数组,例如:
[
{ "title": "JavaScript: The Good Parts", "author": "Douglas Crockford",
"releaseDate": "2008", "keywords": "JavaScript Programming" },
{ "title": "The Little Book on CoffeeScript", "author": "Alex MacCaw",
"releaseDate": "2012", "keywords": "CoffeeScript Programming" },
{ "title": "Scala for the Impatient", "author": "Cay S. Horstmann",
"releaseDate": "2012", "keywords": "Scala Programming" },
{ "title": "American Psycho", "author": "Bret Easton Ellis",
"releaseDate": "1991", "keywords": "Novel Splatter" },
{ "title": "Eloquent JavaScript", "author": "Marijn Haverbeke",
"releaseDate": "2011", "keywords": "JavaScript Programming" }
] -
或者可能是单个对象,例如:
{ "title": "Eloquent JavaScript", "author": "Marijn Haverbeke",
"releaseDate": "2011", "keywords": "JavaScript Programming" }
第二个任务是将映射切片转换为JSON字符串。
然而,我在这两个过程中都没有成功。
我知道使用json
包可以对结构体执行这两个任务。
由于设计上的考虑,我不应该预先编码结构体。
在Go语言中,是否有已知的方法可以实现这些功能?
英文:
I am trying to convert a json string from an http request to a slice of map/s.
And I should also convert a slice of map/s to json string to use for a http response.
I want to convert the followings into a slice of map/s, in both cases below.
The json string from the http request may be an array of several same key value json objects, like;
[
{ title: 'JavaScript: The Good Parts', author: 'Douglas Crockford',
releaseDate: '2008', keywords: 'JavaScript Programming' },
{ title: 'The Little Book on CoffeeScript', author: 'Alex MacCaw',
releaseDate: '2012', keywords: 'CoffeeScript Programming' },
{ title: 'Scala for the Impatient', author: 'Cay S. Horstmann',
releaseDate: '2012', keywords: 'Scala Programming' },
{ title: 'American Psycho', author: 'Bret Easton Ellis',
releaseDate: '1991', keywords: 'Novel Splatter' },
{ title: 'Eloquent JavaScript', author: 'Marijn Haverbeke',
releaseDate: '2011', keywords: 'JavaScript Programming' }
]
or a single one like;
{ title: 'Eloquent JavaScript', author: 'Marijn Haverbeke',
releaseDate: '2011', keywords: 'JavaScript Programming' }
And the second task is to convert a slice of map/s to a json string.
However, I could not mange to succeed in these two procedures.
json package makes it possible to do these two tasks for structs, I am aware of it.
I should not use structs coded beforehand, for a design concern.
Is there a known way to do these in Go.
答案1
得分: 2
你真的可以使用map[string]interface{}
,它可以很好地处理{ "title": ....., "keywords": ["CoffeeScript", "Programming"] }
。
你需要使用类似以下的代码:
for i := 0; i < len(b); i++ {
fmt.Printf("%s by %s was release at %s\n", b[i]["title"], b[i]["author"])
switch v := b[i]["keywords"].(type) {
case []interface{}:
for i := 0; i < len(v); i++ {
switch v := v[i].(type) {
case string:
fmt.Println("\tstring in a slice", v)
case float64: //numbers in json are float64 by default
fmt.Println("\tnumber in a slice", v)
default:
fmt.Printf("\tunknown type (%T)", v, v)
}
}
case string:
fmt.Println("\tstring", v)
}
}
英文:
You really can use use map[string]interface{}
and that would work with { "title": ....., "keywords": ["CoffeeScript", "Programming"] }
just fine
You'd have to use something like :
for i := 0; i < len(b); i++ {
fmt.Printf("%s by %s was release at %s\n", b[i]["title"], b[i]["author"])
switch v := b[i]["keywords"].(type) {
case []interface{}:
for i := 0; i < len(v); i++ {
switch v := v[i].(type) {
case string:
fmt.Println("\tstring in a slice", v)
case float64: //numbers in json are float64 by default
fmt.Println("\tnumber in a slice", v)
default:
fmt.Printf("\tunknown type (%T)", v, v)
}
}
case string:
fmt.Println("\tstring", v)
}
}
答案2
得分: 1
你应该使用json.NewDecoder
将JSON转换为映射切片,并使用json.NewEncoder
将映射切片转换为JSON。
我在这里写了一个示例:http://play.golang.org/p/jbXYGC5pp2
请查看注释中的解释。
希望这对你有帮助。
英文:
you should use json.NewDecoder
to convert JSON to slice of maps and json.NewEncoder
to convert slice of maps into JSON.
i wrote it here http://play.golang.org/p/jbXYGC5pp2
see explanation in the comment.
i hope this help
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论