英文:
Parsing Json In Golang
问题
一直在尝试使用Go语言解析这个JSON文件,以获取给定城市的最低和最高温度。
{
    "data": {
        "current_condition": [
            {
                "cloudcover": "25",
                "humidity": "56",
                "observation_time": "01:33 PM",
                "precipMM": "0.0",
                "pressure": "1016",
                "temp_C": "20",
                "temp_F": "68",
                "visibility": "10",
                "weatherCode": "116",
                "weatherDesc": [
                    {
                        "value": "Partly Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                    }
                ],
                "winddir16Point": "SSW",
                "winddirDegree": "210",
                "windspeedKmph": "7",
                "windspeedMiles": "4"
            }
        ],
        "request": [
            {
                "query": "London, United Kingdom",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2014-09-07",
                "precipMM": "0.0",
                "tempMaxC": "23",
                "tempMaxF": "74",
                "tempMinC": "10",
                "tempMinF": "49",
                "weatherCode": "119",
                "weatherDesc": [
                    {
                        "value": "Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
                    }
                ],
                "winddir16Point": "N",
                "winddirDegree": "355",
                "winddirection": "N",
                "windspeedKmph": "9",
                "windspeedMiles": "6"
            }
        ]
    }
}
已经成功使用结构体和解码JSON字符串。现在我想尝试使用映射,例如map[string]interface{}。
如果u的类型是map[string]interface{},并且将JSON解析为u,则u["data"].(map[string]interface{})["weather"]将给出以下值:
http://api.worldweatheronline.com/free/v1/weather.ashx?q=london&format=json&num_of_days=1&key=8c52bb73c5f6160f5f3aa535d22184638372d22b [map[tempMaxC:23 tempMaxF:74 tempMinC:10 tempMinF:49 winddirection:N windspeedMiles:6 date:2014-09-07 precipMM:0.0 weatherCode:119 winddir16Point:N winddirDegree:355 weatherDesc:[map[value:Cloudy]] weatherIconUrl:[map[value:http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]] windspeedKmph:9]]
而u["data"].(map[string]interface{})["weather"].(map[string]interface{})["tempMinC"]会导致panic: interface conversion: interface is []interface {}, not map[string]interface {}。
有人能解释一下发生了什么吗?
英文:
Have been trying to parse this json file using go to get the Minimum and Maximum Temperature
of a given city.
{
    "data": {
        "current_condition": [
            {
                "cloudcover": "25",
                "humidity": "56",
                "observation_time": "01:33 PM",
                "precipMM": "0.0",
                "pressure": "1016",
                "temp_C": "20",
                "temp_F": "68",
                "visibility": "10",
                "weatherCode": "116",
                "weatherDesc": [
                    {
                        "value": "Partly Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                    }
                ],
                "winddir16Point": "SSW",
                "winddirDegree": "210",
                "windspeedKmph": "7",
                "windspeedMiles": "4"
            }
        ],
        "request": [
            {
                "query": "London, United Kingdom",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2014-09-07",
                "precipMM": "0.0",
                "tempMaxC": "23",
                "tempMaxF": "74",
                "tempMinC": "10",
                "tempMinF": "49",
                "weatherCode": "119",
                "weatherDesc": [
                    {
                        "value": "Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
                    }
                ],
                "winddir16Point": "N",
                "winddirDegree": "355",
                "winddirection": "N",
                "windspeedKmph": "9",
                "windspeedMiles": "6"
            }
        ]
    }
}  
Have been successful using structures and decoding the json string.
Now i want to try using maps, such as map[string]interface{}
If u is of type map[string]interface{} and the json is parsed into u,
u["data"].(map[string]interface{})["weather"]
gives the value
http://api.worldweatheronline.com/free/v1/weather.ashx?q=london&format=json&num_of_days=1&key=8c52bb73c5f6160f5f3aa535d22184638372d22b [map[tempMaxC:23 tempMaxF:74 tempMinC:10 tempMinF:49 winddirection:N windspeedMiles:6 date:2014-09-07 precipMM:0.0 weatherCode:119 winddir16Point:N winddirDegree:355 weatherDesc:[map[value:Cloudy]] weatherIconUrl:[map[value:http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png]] windspeedKmph:9]]
whereas u["data"].(map[string]interface{})["weather"].(map[string]interface{})["tempMinC"],
gives me an panic: interface conversion: interface is []interface {}, not map[string]interface {}
Can someone explain what's happening?
答案1
得分: 0
u["data"].(map[string]interface{})["weather"] 是一个切片(slice)的映射(map),而不是一个映射(map),所以你需要做类似以下的操作:
maps, ok := u["data"].(map[string]interface{})["weather"].([]interface{})
if !ok {
    panic("bad json")
}
for _, m := range maps {
    if m, ok := m.(map[string]interface{}); ok {
        fmt.Println(m["tempMinC"])
    }
}
根据你的 JSON 示例,weather 是一个对象数组,所以在 Go 中对应的是一个切片的映射([]map[string]interface{})。
英文:
u["data"].(map[string]interface{})["weather"] is a slice of maps, not a map, so you would have to do something like :
maps, ok := u["data"].(map[string]interface{})["weather"].([]interface{})
if !ok {
     panic("bad json")
}
for _, m := range maps {
     if m, ok := m.(map[string]interface{}); ok {
          fmt.Println(m["tempMinC"])
     }
}
From your JSON example, weather is an array of objects so this translates to a slice of maps ([]map[string]interface{})in Go.
答案2
得分: 0
这是翻译好的内容:
这是一个切片而不是一个映射。
对u["data"].(map[string]interface{})["weather"]进行范围操作。
英文:
It's a slice not a map.
range the u["data"].(map[string]interface{})["weather"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论