首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从weather-api获取json。iOS

无法从weather-api获取json。iOS
EN

Stack Overflow用户
提问于 2017-01-20 18:56:03
回答 1查看 229关注 0票数 0

这是JSON文件

代码语言:javascript
复制
 {
  "weather":[  
    {  
     "id":804,
     "main":"Clouds",
     "description":"overcast clouds",
     "icon":"04d"
  }
],
 "main":{  
  "temp":273.15,
  "pressure":1035,
  "humidity":84,
  "temp_min":273.15,
  "temp_max":273.15
},

"name":"Wroclaw",

 }

以下是代码。

代码语言:javascript
复制
var cityName: String!
var degree: Int!
var someWeather: String!

let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]


            if let main = json["main"] as? [String : AnyObject] {
                if let temp = main["temp"] as? Int {
                    self.degree = temp
                }
            }
            if let city = json["name"] as? String {
                self.cityName = city
            }
}

     if let weather = json["weather"] as? [String : AnyObject] {
     if let someWeather = weather["main"] as? String {
     self.weatherDescription = someWeather 
} 
} 

labelWeather.text = self.weatherDescription

如何将天气中的"main“或"description”值保存到变量weatherDescription中?我试着像在这段代码中一样,但它没有显示任何东西。学位和城市名称可以正确显示,但天气不起作用。

更新代码/

代码语言:javascript
复制
  var  let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]


                if let main = json["main"] as? [String : AnyObject] {
                    if let temp = main["temp"] as? Int {
                        self.degree = temp
                    }
                    if let pressuer = main["pressure"] as? Int {
                        self.cisnienie = pressuer
                    }
                }

                if let weather = json["weather"] as? [[String : Any]] {
                    for data in weather {
                        if let main = data["main"] as? String {
                            print(main)
                        }
                    }
                }

                if let nazwa = json["name"] as? String {
                    self.nazwaMiasta = nazwa
                }

以下是所有JSON文件:

代码语言:javascript
复制
{  
   "coord":{  
      "lon":-0.13,
      "lat":51.51
   },
   "weather":[  
      {  
         "id":800,
         "main":"Clear",
         "description":"clear sky",
         "icon":"01d"
      }
  ],
  "base":"stations",
  "main":{  
     "temp":279.07,
     "pressure":1032,
     "humidity":52,
     "temp_min":278.15,
     "temp_max":280.15
  },
    "visibility":10000,
     "wind":{  
     "speed":7.2,
     "deg":80
  },
   "clouds":{  
    "all":0
 },
  "dt":1484923800,
 "sys":{  
     "type":1,
     "id":5091,
     "message":0.0087,
     "country":"GB",
     "sunrise":1484898825,
     "sunset":1484929819
  },
  "id":2643743,
  "name":"London",
  "cod":200
}
EN

回答 1

Stack Overflow用户

发布于 2017-01-20 18:59:55

"weather“是一个array,而不是dictionary

代码语言:javascript
复制
if let weather = json["weather"] as? [[String : Any]] {
    for data in weather{
       if let main = data["main"] as? String{
           print(main)
       }
    }
} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41761939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档