英文:
Golang JSON to Slice of Maps of Maps themselves Containing Slices (without Types)
问题
我有一个名为config.json
的文件,格式如下:
{
"recordType1": [
"field1", "field2", "field3", "field4", "field5"
],
"recordType2": [
"field1", "field2", "field3", "field4", "field5", "field6", "field7"
]
}
如果可能的话,我想将其转换为一个切片(slice)的映射(map),其中每个映射本身是一个单独的键(key),而值(value)是一个切片。
我可以手动完成这个操作,像这样:
package main
import "fmt"
func main() {
m := make(map[string][]string, 0)
m2 := make(map[string][]string, 0)
sliceOfMaps := make([]map[string][]string, 0)
m["recordType1"] = []string{"field1", "field2", "field3"}
m2["recordType2"] = []string{"field1", "field2", "field3", "field4", "field5"}
sliceOfMaps = append(sliceOfMaps, m, m2)
fmt.Println(m)
fmt.Println(m2)
fmt.Println(sliceOfMaps)
}
相反,我想对config.json
的内容调用json.Unmarshal
,将该JSON解析为相同的结构。
我尝试过以下方法:
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
"log"
)
func main() {
file, err := ioutil.ReadFile("config/config.json")
if err != nil {
log.Fatal("读取文件错误:", err)
}
var sliceOfMaps []map[string][]string
sliceOfMaps = make([]map[string][]string, 0)
err = json.Unmarshal(file, &sliceOfMaps)
if err != nil {
log.Fatal("解析JSON错误", err)
}
fmt.Println(sliceOfMaps)
}
这会产生错误:
解析JSON错误:json: cannot unmarshal object into Go value of type []map[string][]string
非常感谢您的帮助,欢迎提出澄清问题。谢谢。
英文:
I have a config.json
with the following format:
{
"recordType1": [
"field1", "field2", "field3", "field4", "field5"
],
"recordType2": [
"field1", "field2", "field3", "field4", "field5", "field6", "field7"
]
}
If possible, I'd like to convert this to a slice of maps, where each map is itself is a single 'key', and the 'value' is a slice.
I can accomplish this manually like:
package main
import ("fmt")
func main() {
m := make(map[string][]string, 0)
m2 := make(map[string][]string, 0)
sliceOfMaps := make([]map[string][]string, 0)
m["recordType1"] = []string{"field1", "field2", "field3"}
m2["recordType2"] = []string{"field1", "field2", "field3", "field4", "field5"}
sliceOfMaps = append(sliceOfMaps, m, m2)
fmt.Println(m)
fmt.Println(m2)
fmt.Println(sliceOfMaps)
}
Instead, I'd like to invoke json.Unmarshal
on the contents of config.json
to parse that json into the very same structure.
What I've tried so far:
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
"log"
)
func main() {
file, err := ioutil.ReadFile("config/config.json")
if err != nil {
log.Fatal("Err Reading File:", err)
}
var sliceOfMaps []map[string][]string
sliceOfMaps = make([]map[string][]string, 0)
err = json.Unmarshal(file, &sliceOfMaps)
if err != nil {
log.Fatal("Err Parsing JSON", err)
}
fmt.Println(sliceOfMaps)
}
This produces error:
Err Parsing JSONjson: cannot unmarshal object into Go value of type []map[string][]string
Any assistance is greatly appreciated, clarifying questions welcome. Thanks.
答案1
得分: 4
你的JSON输入和"manual"示例都使用了切片的映射,而不是映射的切片。将目标类型更改为map[string][]string
,然后你就可以运行了:
package main
import (
"fmt"
"encoding/json"
)
var file = []byte(`{
"recordType1": [
"field1", "field2", "field3", "field4", "field5"
],
"recordType2": [
"field1", "field2", "field3", "field4", "field5", "field6", "field7"
]
}`)
func main() {
output := map[string][]string{}
if err := json.Unmarshal(file, &output); err != nil {
panic(err)
}
fmt.Println(output)
}
产生以下输出:
map[recordType1:[field1 field2 field3 field4 field5] recordType2:[field1 field2 field3 field4 field5 field6 field7]]
英文:
Both your JSON input, and your "manual" example are using maps of slices, not slices of maps. Change your target type to map[string][]string
and you should be good to go:
package main
import (
"fmt"
"encoding/json"
)
var file = []byte(`{
"recordType1": [
"field1", "field2", "field3", "field4", "field5"
],
"recordType2": [
"field1", "field2", "field3", "field4", "field5", "field6", "field7"
]
}`)
func main() {
output := map[string][]string{}
if err := json.Unmarshal(file, &output); err != nil {
panic(err)
}
fmt.Println(output)
}
Produces the following output:
map[recordType1:[field1 field2 field3 field4 field5] recordType2:[field1 field2 field3 field4 field5 field6 field7]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论