在尝试迭代JSON数组的映射时,出现了Golang接口转换错误。

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

Golang interface conversion error when trying to iterate through map of json array

问题

我在尝试遍历一些 JSON 数据的映射时遇到了问题。

原始的 JSON 数据如下所示:

"dataArray": [
    {
      "name": "default",
      "url": "/some/url"
    },
    {
      "name": "second",
      "url": "/another/url"
    }
]

映射的样子如下所示:

[map[name:default url:/some/url] map[name:second url:/another/url]]

代码如下所示:

for _, urlItem := range item.(map[string]interface{}){
   做一些操作
}

当 JSON 中是一个对象时,这通常是有效的,但是这里的 JSON 是一个数组,我得到了以下错误:

panic: interface conversion: interface {} is []interface {}, not map[string]interface {}

非常感谢任何帮助!

英文:

I'm having an issue when I'm try to iterate through a map of some json.

The original JSON data looks like this:

"dataArray": [
    {
      "name": "default",
      "url": "/some/url"
    },
    {
      "name": "second",
      "url": "/another/url"
    }
]

the map looks like this:

[map[name:default url:/some/url] map[name:second url:/another/url]]

The code looks like this:

for _, urlItem := range item.(map[string]interface{}){
   do some stuff
}

This normally works when it's a JSON object, but this is an array in the JSON and I get the following error:

> panic: interface conversion: interface {} is []interface {}, not map[string]interface {}

Any help would be greatly appreciated

答案1

得分: 4

错误是:

panic: 接口转换:interface {} 是 []interface {},而不是 map[string]interface {}

在你的代码中,你将 item 转换为 map[string]interface{}

for _, urlItem := range item.(map[string]interface{}) {
   // 做一些操作
}

但实际上 item[]interface {}:将你的转换类型更改为这个。

因为你可以看到你的结果数据是:

[map[name:default url:/some/url] map[name:second url:/another/url]]

它是一个包含 maparray,而不是 map

首先,你可以将数据转换为 []interface{},然后获取该索引并将其转换为 map[string]interface{}。所以一个示例看起来像这样:

data := item.([]interface{})
for _, value := range data {
  yourMap := value.(map[string]interface{})
  // name 值
  name := yourMap["name"].(string) // 以此类推
}
英文:

The error is :

> panic: interface conversion: interface {} is []interface {}, not
> map[string]interface {}

in your code you're converting item into map[string]interface{} :

for _, urlItem := range item.(map[string]interface{}){
   do some stuff
}

But the actual item is []interface {} : change your covert type to this.

Because as you can see your result data is :

[map[name:default url:/some/url] map[name:second url:/another/url]]

it is an array that has map. not map.

First you can convert your data to []interface{} and then get the index of that and convert it to map[string]interface{}.
so an example will look like this :

data := item.([]interface{})
for _,value := range data{
  yourMap := value.(map[string]interface{})
  //name value
  name := yourMap["name"].(string) // and so on
}

huangapple
  • 本文由 发表于 2017年3月30日 23:16:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/43121881.html
匿名

发表评论

匿名网友

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

确定