![](/img/trans.png)
[英]json: cannot unmarshal string into Go value of type map[string]interface {}
[英]How to multiple sort a map[string]interface{} type in go lang?
场景:假设我有一个 JSON 数据要在 go lang 中处理现在我正在使用map[string]interface{}
类型使用 package encoding/json
通过执行marshal/unmarshal
下面是JSON的数据:
{
"MysoreCity": {
"Population": 1000,
"VehicleCount": 1700,
"Temperature": 33
},
"BangaloreCity": {
"Population": 1000,
"VehicleCount": 3500,
"Temperature": 33
},
"KolarCity": {
"Population": 1250,
"VehicleCount": 3500,
"Temperature": 31
},
"TumkurCity": {
"Population": 800,
"VehicleCount": 300,
"Temperature": 29
}
}
现在我想根据优先级执行多排序降序,比如优先级是Temperature
, Population
, VehicleCount
那么我希望 output 像
{
"BangaloreCity": {
"Population": 1000,
"VehicleCount": 3500,
"Temperature": 33
},
"MysoreCity": {
"Population": 1000,
"VehicleCount": 1700,
"Temperature": 33
},
"KolarCity": {
"Population": 1250,
"VehicleCount": 3500,
"Temperature": 31
},
"TumkurCity": {
"Population": 800,
"VehicleCount": 300,
"Temperature": 29
}
}
因此,根据一些动态的优先级进行排序。
问题:我不知道如何在 go lang 中对其进行排序。 我是 go lang 的新手,在搜索对我的数据进行排序时发现了一些东西; 如下所述(来源:链接)
使用范围循环迭代 map 时,未指定迭代顺序,并且不保证从一次迭代到下一次迭代是相同的。
问题:谁能阐明如何对类似的 JSON 数据进行这种排序?
这个答案更多地是关于解决此问题的方法,而不是特定的解决方案。 我认为这是您目前更需要的,因为您还没有接近实际的解决方案。 这是简单的程序步骤;
1)为此定义一个类型,我从这里开始将其称为City
;
"TumkurCity": {
"Population": 800,
"VehicleCount": 300,
"Temperature": 29
}
type City struct {
Population int
VehicleCount int
Temperature int
Name string
}
2)解组map[string]City
3)使用类似的方法来转换数据;
func ToSlice(m map[string]City) []City {
cities := make([]City, 0, len(m))
for k, v := range m {
v.Name = k
cities = append(cities, v)
}
return cities
}
4)定义一些方法来对城市进行排序
我在浏览器中编写了该代码,因此未经过任何测试。 它应该提供如何处理它的充分示例。 取消编组时,您的城市类型将没有名称值,因此您以后可以为它分配密钥(城市名称)。 将它们放在切片或阵列中,然后进行排序。 如果遵循这些步骤,则非常简单。
我执行了此功能,从N个图及其排序键中获取了一个结果图。
func getSortedKeys(maps ...map[string]string) (map[string]string, []string) {
var keys []string
resultMap := map[string]string{}
for _, currMap := range maps {
for k, v := range currMap {
resultMap[k] = v
keys = append(keys, k)
}
}
sort.Strings(keys)
return resultMap, keys
}
在功能之外,您可以执行以下操作:
concatenedMaps, keys := getSortedKeys(map1,map2)
for _, k := range keys {
value := concatenedMaps[k]
}
你可以使用https://github.com/rxwycdh/rxhash rxhash.SortMap
,可以按键对嵌套复杂的 map 进行排序。
例如:
import "github.com/rxwycdh/rxhash"
dd := map[string]any{
"MysoreCity": map[string]any{
"Population": 1000,
"VehicleCount": 1700,
"Temperature": 33,
},
"BangaloreCity": map[string]any{
"Population": 1000,
"VehicleCount": 3500,
"Temperature": 33,
},
"KolarCity": map[string]any{
"Population": 1250,
"VehicleCount": 3500,
"Temperature": 31,
},
"TumkurCity": map[string]any{
"Population": 800,
"VehicleCount": 300,
"Temperature": 29,
},
}
b, _ := json.Marshal(rxhash.SortMap(dd))
println(string(b))
output:
{
"BangaloreCity": {
"Population": 1000,
"Temperature": 33,
"VehicleCount": 3500
},
"KolarCity": {
"Population": 1250,
"Temperature": 31,
"VehicleCount": 3500
},
"MysoreCity": {
"Population": 1000,
"Temperature": 33,
"VehicleCount": 1700
},
"TumkurCity": {
"Population": 800,
"Temperature": 29,
"VehicleCount": 300
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.