将JSON顶层数组解组为字符串到字符串的映射。

huangapple go评论128阅读模式
英文:

Unmarshaling JSON top level array into map of string to string

问题

我正在尝试解析以下类型的JSON数组:

  1. [
  2. {"abc's": "n;05881364"},
  3. {"abcoulomb": "n;13658345"},
  4. {"abcs": "n;05881364"}
  5. ]

解析成map[string]string。这个问题 https://stackoverflow.com/questions/25465566/golang-parse-json-array-into-data-structure 几乎回答了我的问题,但我的是一个真正的map,而不是一个map的数组。解析成[]map[string]string是可以的,但现在我得到的是一个map[string]string的map,而不是一个简单的mapstring

英文:

I'm trying to unmarshal a JSON array of the following type:

  1. [
  2. {"abc's": "n;05881364"},
  3. {"abcoulomb": "n;13658345"},
  4. {"abcs": "n;05881364"}
  5. ]

into a map[string]string. This question https://stackoverflow.com/questions/25465566/golang-parse-json-array-into-data-structure almost answered my problem, but mine is a truly map, not an array of maps. Unmarshaling into a []map[string]string worked but I now get a map of map[string]string, not a simple map of string as it should be

答案1

得分: 7

使用json包无法直接完成此操作;您需要自己进行转换。这很简单:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func main() {
  7. data := []byte(`
  8. [
  9. {"abc's": "n;05881364"},
  10. {"abcoulomb": "n;13658345"},
  11. {"abcs": "n;05881364"}
  12. ]
  13. `)
  14. var mapSlice []map[string]string
  15. if err := json.Unmarshal(data, &mapSlice); err != nil {
  16. panic(err)
  17. }
  18. resultingMap := map[string]string{}
  19. for _, m := range mapSlice {
  20. for k, v := range m {
  21. resultingMap[k] = v
  22. }
  23. }
  24. fmt.Println(resultingMap)
  25. }

输出结果:

  1. map[abc's:n;05881364 abcoulomb:n;13658345 abcs:n;05881364]
英文:

There is no way to do it directly with the json package; you have to do the conversion yourself. This is simple:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func main() {
  7. data := []byte(`
  8. [
  9. {"abc's": "n;05881364"},
  10. {"abcoulomb": "n;13658345"},
  11. {"abcs": "n;05881364"}
  12. ]
  13. `)
  14. var mapSlice []map[string]string
  15. if err := json.Unmarshal(data, &mapSlice); err != nil {
  16. panic(err)
  17. }
  18. resultingMap := map[string]string{}
  19. for _, m := range mapSlice {
  20. for k, v := range m {
  21. resultingMap[k] = v
  22. }
  23. }
  24. fmt.Println(resultingMap)
  25. }

Output

  1. map[abc's:n;05881364 abcoulomb:n;13658345 abcs:n;05881364]

答案2

得分: 3

一个与Alex的答案(虽然非常相似)类似的替代方法是定义自己的类型,并附带一个UnmarshalJSON函数。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type myMapping map[string]string
  7. func (mm myMapping) UnmarshalJSON(b []byte) error {
  8. var temp []map[string]string
  9. if err := json.Unmarshal(b, &temp); err != nil {
  10. return err
  11. }
  12. for _, m := range temp {
  13. for k, v := range m {
  14. mm[k] = v
  15. }
  16. }
  17. return nil
  18. }
  19. func main() {
  20. data := []byte(`
  21. [
  22. {"abc's": "n;05881364"},
  23. {"abcoulomb": "n;13658345"},
  24. {"abcs": "n;05881364"}
  25. ]`)
  26. resultingMap := myMapping{}
  27. if err := json.Unmarshal(data, &resultingMap); err != nil {
  28. panic(err)
  29. }
  30. fmt.Println(resultingMap)
  31. }

Playground

英文:

An alternative (though very similar) to Alex's answer is to define your own type along with an UnmarshalJSON function.

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type myMapping map[string]string
  7. func (mm myMapping) UnmarshalJSON(b []byte) error {
  8. var temp []map[string]string
  9. if err := json.Unmarshal(b, &temp); err != nil {
  10. return err
  11. }
  12. for _, m := range temp {
  13. for k, v := range m {
  14. mm[k] = v
  15. }
  16. }
  17. return nil
  18. }
  19. func main() {
  20. data := []byte(`
  21. [
  22. {"abc's": "n;05881364"},
  23. {"abcoulomb": "n;13658345"},
  24. {"abcs": "n;05881364"}
  25. ]`)
  26. resultingMap := myMapping{}
  27. if err := json.Unmarshal(data, &resultingMap); err != nil {
  28. panic(err)
  29. }
  30. fmt.Println(resultingMap)
  31. }

Playground

huangapple
  • 本文由 发表于 2017年2月17日 12:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/42289591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定