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

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

Unmarshaling JSON top level array into map of string to string

问题

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

[
    {"abc's": "n;05881364"},
    {"abcoulomb": "n;13658345"},
    {"abcs": "n;05881364"}
]

解析成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:

[
{"abc's": "n;05881364"},
{"abcoulomb": "n;13658345"},
{"abcs": "n;05881364"}
]

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包无法直接完成此操作;您需要自己进行转换。这很简单:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	data := []byte(`
        [
        {"abc's": "n;05881364"},
        {"abcoulomb": "n;13658345"},
        {"abcs": "n;05881364"}
        ]
    `)

	var mapSlice []map[string]string
	if err := json.Unmarshal(data, &mapSlice); err != nil {
		panic(err)
	}
	resultingMap := map[string]string{}
	for _, m := range mapSlice {
		for k, v := range m {
			resultingMap[k] = v
		}
	}
	fmt.Println(resultingMap)
}

输出结果:

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:

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	data := []byte(`
        [
        {"abc's": "n;05881364"},
        {"abcoulomb": "n;13658345"},
        {"abcs": "n;05881364"}
        ]
    `)

	var mapSlice []map[string]string
	if err := json.Unmarshal(data, &mapSlice); err != nil {
		panic(err)
	}
	resultingMap := map[string]string{}
	for _, m := range mapSlice {
		for k, v := range m {
			resultingMap[k] = v
		}
	}
	fmt.Println(resultingMap)
}

Output

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

答案2

得分: 3

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

package main

import (
	"encoding/json"
	"fmt"
)

type myMapping map[string]string

func (mm myMapping) UnmarshalJSON(b []byte) error {
	var temp []map[string]string
	if err := json.Unmarshal(b, &temp); err != nil {
		return err
	}
	for _, m := range temp {
		for k, v := range m {
			mm[k] = v
		}
	}
	return nil
}

func main() {
	data := []byte(`
		[
			{"abc's": "n;05881364"},
			{"abcoulomb": "n;13658345"},
			{"abcs": "n;05881364"}
		]`)

	resultingMap := myMapping{}
	if err := json.Unmarshal(data, &resultingMap); err != nil {
		panic(err)
	}
	fmt.Println(resultingMap)
}

Playground

英文:

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

package main

import (
	"encoding/json"
	"fmt"
)

type myMapping map[string]string

func (mm myMapping) UnmarshalJSON(b []byte) error {
	var temp []map[string]string
	if err := json.Unmarshal(b, &temp); err != nil {
		return err
	}
	for _, m := range temp {
		for k, v := range m {
			mm[k] = v
		}
	}
	return nil
}

func main() {
	data := []byte(`
        	[
        		{"abc's": "n;05881364"},
        		{"abcoulomb": "n;13658345"},
        		{"abcs": "n;05881364"}
        	]`)

	resultingMap := myMapping{}
	if err := json.Unmarshal(data, &resultingMap); err != nil {
		panic(err)
	}
	fmt.Println(resultingMap)
}

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:

确定