用Swift 3解析失败

时间:2017-04-18 13:51:43

标签: swift parsing json-serialization

我遇到了无法解析数据的DarkSkyApi问题。当我控制数据时它是可靠的并且没有连接错误,但JSONSerialization无法正常工作。是否有任何建议来解决问题。

{     "目前":{     "时间":1453402675,     "摘要":" Rain",     " icon":" rain",     " nearestStormDistance":0,     " precipIntensity":0.1685,     " precipIntensityError":0.0067,     " degraProbability":1,     " precipType":" rain",     "温度":48.71,     " apparentTemperature":46.93,     "露点":47.7,     "湿度":0.96,     " windSpeed":4.64,     " windBearing":186,     "能见度":4.3,     " cloudCover":0.73,     "压力":1009.7,     "臭氧":328.35}}

if httpResponse.statusCode == 200 {       
    if let data = data {
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
            completion(json,nil)
            print("laaan")
        } catch {
            completion(nil, .jsonConversionFailure)
        }
    } else {
        completion(nil, .invalidData)
    }
} else {
    completion(nil, DarkSykError.responseUnsuccessful)
}

客户方:

DispatchQueue.main.async {
    guard let json = json else {
        completion(nil , error)
        return
    }

    guard let currentWeatherJson = json ["currently"] as? [String : AnyObject] , let currentWeather = CurrentWeather(json : currentWeatherJson) else {
        print("laaan")
        completion(nil , .jsonParsingFailure)
        return
    }

    completion(currentWeather, nil )
}

0 个答案:

没有答案