Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoder error #113

Open
a5978445 opened this issue Jun 13, 2018 · 0 comments
Open

decoder error #113

a5978445 opened this issue Jun 13, 2018 · 0 comments

Comments

@a5978445
Copy link

source son file:

{
	"status": "0",
	"msg": "ok",
	"result": {
		"city": "北京",
		"cityid": "1",
		"citycode": "101010100",
		"date": "2018-06-13",
		"week": "星期三",
		"weather": "",
		"temp": "17",
		"temphigh": "28",
		"templow": "18",
		"img": "301",
		"humidity": "84",
		"pressure": "1000",
		"windspeed": "10.4",
		"winddirect": "东风",
		"windpower": "2级",
		"updatetime": "2018-06-13 13:40:00",
		"index": [{
			"iname": "空调指数",
			"ivalue": "较少开启",
			"detail": "您将感到很舒适,一般不需要开启空调。"
		}, {
			"iname": "运动指数",
			"ivalue": "较不宜",
			"detail": "有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意保暖并携带雨具。"
		}, {
			"iname": "紫外线指数",
			"ivalue": "",
			"detail": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"
		}, {
			"iname": "感冒指数",
			"ivalue": "少发",
			"detail": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。"
		}, {
			"iname": "洗车指数",
			"ivalue": "不宜",
			"detail": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
		}, {
			"iname": "空气污染扩散指数",
			"ivalue": "",
			"detail": "气象条件有利于空气污染物稀释、扩散和清除,可在室外正常活动。"
		}, {
			"iname": "穿衣指数",
			"ivalue": "",
			"detail": "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"
		}],
		"aqi": {
			"so2": "3",
			"so224": "4",
			"no2": "14",
			"no224": "35",
			"co": "0.430",
			"co24": "1.030",
			"o3": "105",
			"o38": "88",
			"o324": "100",
			"pm10": "18",
			"pm1024": "71",
			"pm2_5": "10",
			"pm2_524": "70",
			"iso2": "2",
			"ino2": "7",
			"ico": "5",
			"io3": "34",
			"io38": "44",
			"ipm10": "21",
			"ipm2_5": "14",
			"aqi": "44",
			"primarypollutant": "O3",
			"quality": "",
			"timepoint": "2018-06-13 13:00:00",
			"aqiinfo": {
				"level": "一级",
				"color": "#00e400",
				"affect": "空气质量令人满意,基本无空气污染",
				"measure": "各类人群可正常活动"
			}
		},
		"daily": [{
			"date": "2018-06-13",
			"week": "星期三",
			"sunrise": "04:45",
			"sunset": "19:43",
			"night": {
				"weather": "多云",
				"templow": "18",
				"img": "1",
				"winddirect": "西北风",
				"windpower": "微风"
			},
			"day": {
				"weather": "雷阵雨",
				"temphigh": "28",
				"img": "4",
				"winddirect": "东北风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-14",
			"week": "星期四",
			"sunrise": "04:45",
			"sunset": "19:44",
			"night": {
				"weather": "多云",
				"templow": "20",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "多云",
				"temphigh": "31",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-15",
			"week": "星期五",
			"sunrise": "04:45",
			"sunset": "19:44",
			"night": {
				"weather": "多云",
				"templow": "22",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "多云",
				"temphigh": "33",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-16",
			"week": "星期六",
			"sunrise": "04:45",
			"sunset": "19:44",
			"night": {
				"weather": "雷阵雨",
				"templow": "21",
				"img": "4",
				"winddirect": "西南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "多云",
				"temphigh": "34",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-17",
			"week": "星期日",
			"sunrise": "04:45",
			"sunset": "19:45",
			"night": {
				"weather": "多云",
				"templow": "20",
				"img": "1",
				"winddirect": "东南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "雷阵雨",
				"temphigh": "30",
				"img": "4",
				"winddirect": "东北风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-18",
			"week": "星期一",
			"sunrise": "04:45",
			"sunset": "19:45",
			"night": {
				"weather": "多云",
				"templow": "20",
				"img": "1",
				"winddirect": "南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "",
				"temphigh": "31",
				"img": "0",
				"winddirect": "东南风",
				"windpower": "微风"
			}
		}, {
			"date": "2018-06-19",
			"week": "星期二",
			"sunrise": "04:45",
			"sunset": "19:45",
			"night": {
				"weather": "多云",
				"templow": "21",
				"img": "1",
				"winddirect": "东南风",
				"windpower": "微风"
			},
			"day": {
				"weather": "多云",
				"temphigh": "30",
				"img": "1",
				"winddirect": "东南风",
				"windpower": "微风"
			}
		}],
		"hourly": [{
			"time": "13:00",
			"weather": "多云",
			"temp": "26",
			"img": "1"
		}, {
			"time": "14:00",
			"weather": "多云",
			"temp": "27",
			"img": "1"
		}, {
			"time": "15:00",
			"weather": "多云",
			"temp": "28",
			"img": "1"
		}, {
			"time": "16:00",
			"weather": "多云",
			"temp": "27",
			"img": "1"
		}, {
			"time": "17:00",
			"weather": "",
			"temp": "27",
			"img": "0"
		}, {
			"time": "18:00",
			"weather": "",
			"temp": "26",
			"img": "0"
		}, {
			"time": "19:00",
			"weather": "",
			"temp": "25",
			"img": "0"
		}, {
			"time": "20:00",
			"weather": "",
			"temp": "24",
			"img": "0"
		}, {
			"time": "21:00",
			"weather": "",
			"temp": "22",
			"img": "0"
		}, {
			"time": "22:00",
			"weather": "",
			"temp": "22",
			"img": "0"
		}, {
			"time": "23:00",
			"weather": "",
			"temp": "22",
			"img": "0"
		}, {
			"time": "0:00",
			"weather": "",
			"temp": "21",
			"img": "0"
		}, {
			"time": "1:00",
			"weather": "",
			"temp": "21",
			"img": "0"
		}, {
			"time": "2:00",
			"weather": "",
			"temp": "20",
			"img": "0"
		}, {
			"time": "3:00",
			"weather": "",
			"temp": "20",
			"img": "0"
		}, {
			"time": "4:00",
			"weather": "",
			"temp": "18",
			"img": "0"
		}, {
			"time": "5:00",
			"weather": "",
			"temp": "19",
			"img": "0"
		}, {
			"time": "6:00",
			"weather": "",
			"temp": "20",
			"img": "0"
		}, {
			"time": "7:00",
			"weather": "",
			"temp": "22",
			"img": "0"
		}, {
			"time": "8:00",
			"weather": "",
			"temp": "23",
			"img": "0"
		}, {
			"time": "9:00",
			"weather": "",
			"temp": "25",
			"img": "0"
		}, {
			"time": "10:00",
			"weather": "",
			"temp": "27",
			"img": "0"
		}, {
			"time": "11:00",
			"weather": "",
			"temp": "28",
			"img": "0"
		}, {
			"time": "12:00",
			"weather": "",
			"temp": "29",
			"img": "0"
		}]
	}
}

when i use jsonExport export swift-codable file. it will generate the code

//
//	RootClass.swift
//
//	Create by 腾芳 李 on 13/6/2018
//	Copyright © 2018. All rights reserved.
//	Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport

import Foundation

struct RootClass : Codable {

	let msg : String?
	let result : Result?
	let status : String?


	enum CodingKeys: String, CodingKey {
		case msg = "msg"
		case result
		case status = "status"
	}
	init(from decoder: Decoder) throws {
		let values = try decoder.container(keyedBy: CodingKeys.self)
		msg = try values.decodeIfPresent(String.self, forKey: .msg)
		result = try Result(from: decoder)
		status = try values.decodeIfPresent(String.self, forKey: .status)
	}


}

above the code result = try Result(from: decoder) has logic error, should be modify result = try values.decodeIfPresent(Result.self, forKey: .result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant