2017-04-01 91 views
0

我與SWIFT的Progrmamming初學者,我需要從JSON值它看起來像這樣:斯威夫特JSON解析Wunderground

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "astronomy": 1 
    } 
    } 
     , "moon_phase": { 
     "percentIlluminated":"23", 
     "ageOfMoon":"5", 
     "phaseofMoon":"Erstes Viertel", 
     "hemisphere":"North", 
     "current_time": { 
     "hour":"8", 
     "minute":"28" 
     }, 
     "sunrise": { 
     "hour":"6", 
     "minute":"55" 
     }, 
     "sunset": { 
     "hour":"19", 
     "minute":"50" 
     }, 
     "moonrise": { 
     "hour":"9", 
     "minute":"50" 
     }, 
     "moonset": { 
     "hour":"0", 
     "minute":"06" 
     } 
    }, 
    "sun_phase": { 
     "sunrise": { 
     "hour":"6", 
     "minute":"55" 
     }, 
     "sunset": { 
     "hour":"19", 
     "minute":"50" 
     } 
    } 
} 

我會從日落和日出到一個變量得到小時&分鐘!

我的代碼:

var sunriseHour: [String] = [] 
    var sunriseMinute: [String] = [] 
    var sunsetHour: [String] = [] 
    var sunsetMinute: [String] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     let url=URL(string:"http://api.wunderground.com/api/dbd539bd631e/astronomy/lang:DL/q/DL/iphofen.json") 
     do { 
      let allData = try Data(contentsOf: url!) 
      let data = try JSONSerialization.jsonObject(with: allData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject] 
      if let arrJSON = data["moon_phase"] { 


         if let sunriseData = arrJSON["sunrise"]{ 



       print(sunriseData) 
             } 
       if let sunsetData = arrJSON["sunset"]{ 

      print(sunsetData) 
       } 
      } 
      print("hour: ",sunriseHour) 
      print("minute: ",sunriseMinute) 
      print("hour: ",sunsetHour) 
      print("minute: ",sunsetMinute) 


     } 
     catch { 

     } 

      } 

i相打印(sunriseData)和印刷(sunsetData)從此正確的數據得到:

可選({ 小時= 6; 分鐘= 55 ;})可選(小時= 19; 分鐘= 50;})小時:[]分鐘:[]小時:[]分鐘:[]

我如何得到它在變量sunriseHour,sunriseMinute,sunsetHour,sunsetMinute ..

謝謝你的幫助..!

回答

0

全部hourminute值是(單)String而不是[String](數組)。

聲明所有變量作爲空字符串

var sunriseHour = "" 
var sunriseMinute = "" 
var sunsetHour = "" 
var sunsetMinute = "" 

那麼就不要從遠程URL同步加載數據,總是使用異步方式:

 let url = URL(string:"http://api.wunderground.com/api/dbd539bd631e/astronomy/lang:DL/q/DL/iphofen.json")! 

     let task = URLSession.shared.dataTask(with: url) { (data, response, error) in 
      if error != nil { 
       print(error!) 
      } else { 
       do { 
        guard let jsonData = try JSONSerialization.jsonObject(with:data!) as? [String:Any], 
          let moonPhase = jsonData["moon_phase"] as? [String:Any] else { return } 

        let sunrise = moonPhase["sunrise"] as! [String:String] 
        let sunset = moonPhase["sunset"] as! [String:String] 

        sunriseHour = sunrise["hour"]! 
        sunriseMinute = sunrise["minute"]! 

        sunsetHour = sunset["hour"]! 
        sunsetMinute = sunset["minute"]! 

        print("Sunrise - \(sunriseHour):\(sunriseMinute)" 
        print("Sunset - \(sunsetHour):\(sunsetMinute)" 

       } catch { 
        print(error) 
       } 
      } 
     } 
     task.resume() 

的數據sunrisesunset在字典源自moon_phase。所有值都被強制解開,因爲日出和日落應該每天都存在;-)

PS:考慮定義更多的描述性變量名稱,arrJSON不是很有意義。

+0

你是我的英雄...... :-)謝謝 – Heisenberg