[英]Error: interface conversion interface {} is []interface {}, not map[string]interface {}
我正在构建一个项目,该项目从用户那里获取一个术语,然后执行谷歌搜索并以 json 格式返回标题列表。
我正在使用 serpwow API 来执行谷歌搜索并试图解析响应。
但是,我收到错误消息:
panic: interface conversion: interface {} is []interface {}, not map[string]interface {}.
我查看了各种表格并试图了解映射的工作原理,但我不确定为什么在这种情况下,我的映射不起作用。 有机结果表如下所示:
"organic_results": [
{
"position": 1,
"title": "The 10 Best Pizza Places in Dublin - TripAdvisor",
"link": "https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html",
"domain": "www.tripadvisor.ie",
"displayed_link": "https://www.tripadvisor.ie › ... › County Dublin › Dublin",
"snippet": "Best Pizza in Dublin, County Dublin: Find TripAdvisor traveller reviews of Dublin Pizza places and search by price, location, and more.",
"prerender": false,
"snippet_matched": [
"Pizza",
"Pizza"
],
"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:OS-Ar9hB_ngJ:https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html+&cd=4&hl=en&ct=clnk&gl=ie",
"related_page_link": "https://www.google.com/search?q=related:https://www.tripadvisor.ie/Restaurants-g186605-c31-Dublin_County_Dublin.html+pizza&tbo=1&sa=X&ved=2ahUKEwicjYKvvNjmAhVoSBUIHa9MBhcQHzADegQIARAH",
"block_position": 2
},
这是我的代码片段:
package main
import (
"fmt"
"strings"
serpwow "github.com/serpwow/google-search-results-golang"
)
func main() {
// set API key
apiKey := "Key_Hidden"
//read term to search
fmt.Print("What term would you like to search in google? ")
var term string
fmt.Scanln(&term)
// set up our search parameters
parameters := map[string]interface{}{
"q": term,
}
// retrieve the search results as JSON
response, error := serpwow.GetJSON(parameters, apiKey)
// print the response, or error, if one occurred
if error != nil {
fmt.Println(error)
} else {
//extract title from organic results
//result := fmt.Sprintf("%v", response["organic_results"].(map[string]interface{})["title"])
for _, item := range response["organic_results"].([]interface{}) {
fmt.Sprintf("%v", item.(map[string]interface{})["title"])
}
//s := []string{result, "\n"}
//fmt.Printf(strings.Join(s, " "))
}
有人可以帮我弄清楚我的逻辑错误在哪里吗?
response["organic_results"]
对应于 JSON 数组 "organic_results",因此它不是一个map[string]interface{}
,而是一个[]interface
。 有多种结果,而不是一种。
for _,item:=range respose["organic_results"].([]interface{}) {
fmt.Printf("%v", item.(map[string]interface{})["title"])
}
正如您在 JSON 中看到的那样, organic_results
的值是一个 JSON 数组,而不是一个对象; 数组的每个元素都是一个对象。 因此,您不能将organic_results
声明为map[string]interface{}
因为正如错误所述,它不是地图,而是[]interface
。 例如,您可以执行以下操作:
result := fmt.Sprintf("%v", response["organic_results"].([]interface{})[0].(map[string]interface{})["title"])
但是当然这只会让你得到第一个结果的标题,如果结果为空,它会崩溃。 您必须将其视为“从响应中获取标题”,而是“处理返回的零个或多个结果”——即,您可能想要遍历organic_results
并对每个结果对象执行某些操作。
有时,逆向工程也有效,虽然我定义的所有类型看起来都非常直观,但我也遇到了无法解组自定义 JSON 文件的相同问题。
type ResourceConfig struct {
ResourceType map[string]AlphabetType `json:"resourceType"`
RedisConfig RedisConfigT `json:"redisConfig"`
}
type AlphabetType struct {
XXX []string `json:"xxx"`
YYY []string `json:"yyy"`
}
type RedisConfigT struct {
Broker string `json:"broker"`
DB string `json:"db"`
}
对于它的 json 看起来像这样:
{
"resourceType": [
{"/abcdefg": [{"xxx": ["/vrf", "/lsp", "/vpn"], "yyy": ["/fpc"]}]},
{"/vrf": [{"xxx": [""], "yyy": ["/abcdefg"]}]},
{"/lsp": [{"xxx": [""], "yyy": ["/abcdefg"]}]},
{"/vpn": [{"xxx": [""], "yyy": ["/abcdefg"]}]},
{"/fpc": [{"xxx": ["/abcdefg"], "yyy": [""]}]}
],
"redisConfig": {"broker": "localhost: 6379", "db": 0}
}
在执行 UnMarshall 时,它抛出无法解析的错误。
所以我决定先以编程方式形成所需的地图,将其编组为 json,然后打印出来。
{"resourceType":{"/fpc":{"XXX":["/abcdefg"],"YYY":[]},
"/abcdefg":{"XXX":["/vrf","/lsp","/vpn"],"YYY":["/fpc"]},
"/abc":{"XXX":[],"YYY":["/abcdefg"]},
"/vpn":{"XXX":[],"YYY":["/abcdefg"]},
"/vrf":{"XXX":[],"YYY":["/abcdefg"]}},
"redisConfig":{"broker":"localhost:6349","db":"0"}}
现在在 json 文件中使用相同的格式并解组它。 它将很好地适应您定义的基于最初生成 Json 的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.