douci6541 2014-09-07 14:07
浏览 43
已采纳

在Golang中解析Json

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?

  • 写回答

2条回答 默认 最新

  • douduan3203 2014-09-07 14:19
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应