在Swift中调用JSONSerialization时获取无效的json

时间:2018-11-08 03:42:34

标签: json swift

我正在尝试访问URL并解析JSON输出。以utf8编码打印JSON 代码:

let urlString:String = "https://developer.nrel.gov/api/.../"
    let pvURL = URL(string: urlString)
    let dataTask = URLSession.shared.dataTask(with:pvURL!) { (data, response, error) in
        do {
            let json = try JSONSerialization.jsonObject(with: data!)
            print(String(data: data!, encoding: .utf8)!)

        }catch let err {
            print("error: \(err.localizedDescription)")
        }
    }
    dataTask.resume()

打印以下输出。我在联机JSON解析器中尝试此JSON,但失败。在第一行本身出现错误。

{"inputs":{"lat":"29.93","lon":"-95.61","system_capacity":"30.30","azimuth":"180","tilt":"40","array_type":"1","module_type":"1","losses":"10"},"errors":[],"warnings":[],"version":"1.0.1","ssc_info":{"version":45,"build":"Linux 64 bit GNU/C++ Jul  7 2015 14:24:09"},"station_info":{"lat":29.93000030517578,"lon":-95.62000274658203,"elev":41.0,"tz":-6.0,"location":"None","city":"","state":"Texas","solar_resource_file":"W9562N2993.csv","distance":964},"outputs":{"ac_monthly":[3480.57373046875,3440.078369140625,3992.6513671875,3977.071533203125,4074.91357421875,3701.75,3897.655517578125,4248.00390625,4023.283447265625,4157.29931640625,3605.156005859375,3342.12890625],"poa_monthly":[139.791015625,140.18896484375,164.8218536376953,164.47149658203125,173.2971649169922,159.90576171875,169.84793090820312,186.20114135742188,173.14492797851562,176.2291717529297,148.28318786621094,136.62326049804688],"solrad_monthly":[4.509387493133545,5.006748676300049,5.316833972930908,5.4823832511901855,5.590230941772461,5.3301920890808105,5.4789652824401855,6.00648832321167,5.77149772644043,5.684812068939209,4.94277286529541,4.407201766967773],"dc_monthly":[3644.867919921875,3606.52001953125,4179.85107421875,4158.3193359375,4252.9140625,3865.03369140625,4069.092041015625,4432.62744140625,4198.369140625,4336.99609375,3767.055419921875,3490.091064453125],"ac_annual":45940.55859375,"solrad_annual":5.293959140777588,"capacity_factor":17.308107376098633}}`

而如果我在浏览器中访问urlString则会提供有效的json:

{
"inputs": {
"lat": "29.93",
"lon": "-95.61",
"system_capacity": "30.30",
"azimuth": "180",
"tilt": "40",
"array_type": "1",
"module_type": "1",
"losses": "10"
},
"errors": [],
"warnings": [],
"version": "1.0.1",
"ssc_info": {
"version": 45,
"build": "Linux 64 bit GNU/C++ Jul  7 2015 14:24:09"
},
"station_info": {
"lat": 29.93000030517578,
"lon": -95.62000274658203,
"elev": 41,
"tz": -6,
"location": "None",
"city": "",
"state": "Texas",
"solar_resource_file": "W9562N2993.csv",
"distance": 964
},
"outputs": {
"ac_monthly": [
3480.57373046875,
3440.078369140625,
3992.6513671875,
3977.071533203125,
4074.91357421875,
3701.75,
3897.655517578125,
4248.00390625,
4023.283447265625,
4157.29931640625,
3605.156005859375,
3342.12890625
],
...
}

1 个答案:

答案 0 :(得分:0)

在您的代码中,您正在将JSON数据转换为JSON对象(数组,字典)。

但是在浏览器中,JSON数据打印为String而不是JSON对象(数组,字典)。

因此,如果您也想在代码中打印JSON字符串,则可以按如下所示进行打印。

let string = String(data: self, encoding: .utf8)
print("JSON String:\(String(describing: string))")

希望有帮助。