将 JSON 转换为自定义对象时出错
Posted
技术标签:
【中文标题】将 JSON 转换为自定义对象时出错【英文标题】:Error casting JSON to custom object 【发布时间】:2015-10-09 09:49:42 【问题描述】:我有一个不为人知的错误。
我尝试使用自定义对象反序列化字典。但它不起作用。
let decodedJson = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, TestCast>
我的对象是(我用objective-c对象进行了测试,但结果相同)
class TestCast: NSObject
var ca:String?
var resultat:String?
var remuneration:String?
var delta_ca:String?
var delta_resultat:String?
var delta_remuneration:String?
我的 json 是:
"201310":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201311":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201312":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201401":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201402":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201403":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201404":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201405":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201406":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201407":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201408":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201409":
"resultat": 0,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 0,
"remuneration": 0,
"delta_ca": "0.00"
,
"201410":
"resultat": 3938.33,
"delta_resultat": "3,938.33",
"delta_remuneration": "-2,700.00",
"ca": 3938.33,
"remuneration": 2700,
"delta_ca": "3,938.33"
,
"201411":
"resultat": 3938.33,
"delta_resultat": "0.00",
"delta_remuneration": "0.00",
"ca": 3938.33,
"remuneration": 2700,
"delta_ca": "0.00"
,
"201412":
"resultat": 34211.1,
"delta_resultat": "30,272.77",
"delta_remuneration": "0.00",
"ca": 34211.1,
"remuneration": 2700,
"delta_ca": "30,272.77"
,
"201501":
"resultat": 7812.89,
"delta_resultat": "-26,398.21",
"delta_remuneration": "2,700.00",
"ca": 13500,
"remuneration": 0,
"delta_ca": "-20,711.10"
,
"201502":
"resultat": -20195.54,
"delta_resultat": "-28,008.43",
"delta_remuneration": "0.00",
"ca": 5700,
"remuneration": 0,
"delta_ca": "-7,800.00"
,
"201503":
"resultat": 2379.71,
"delta_resultat": "22,575.25",
"delta_remuneration": "0.00",
"ca": 4650,
"remuneration": 0,
"delta_ca": "-1,050.00"
,
"201504":
"resultat": 14582.38,
"delta_resultat": "12,202.67",
"delta_remuneration": "0.00",
"ca": 16500,
"remuneration": 0,
"delta_ca": "11,850.00"
,
"201505":
"resultat": 4568.45,
"delta_resultat": "-10,013.93",
"delta_remuneration": "0.00",
"ca": 9200,
"remuneration": 0,
"delta_ca": "-7,300.00"
,
"201506":
"resultat": 9941.67,
"delta_resultat": "5,373.22",
"delta_remuneration": "-49.60",
"ca": 12349.67,
"remuneration": 49.6,
"delta_ca": "3,149.67"
,
"201507":
"resultat": 8138.53,
"delta_resultat": "-1,803.14",
"delta_remuneration": "-90.40",
"ca": 13526.67,
"remuneration": 140,
"delta_ca": "1,177.00"
,
"201508":
"resultat": 3321.47,
"delta_resultat": "-4,817.06",
"delta_remuneration": "140.00",
"ca": 4500,
"remuneration": 0,
"delta_ca": "-9,026.67"
,
"201509":
"resultat": -2454.47,
"delta_resultat": "-5,775.94",
"delta_remuneration": "-23.00",
"ca": 0,
"remuneration": 23,
"delta_ca": "-4,500.00"
我有一个错误:
无法将“__NSCFDictionary”(0x104f04ef0) 类型的值转换为“Pulse.TestCast”
【问题讨论】:
您需要使用一些映射框架将字典转换为您的对象。你不能只是施放它 【参考方案1】:将字典数组类型转换为类不会自动使用字典数据创建此类的实例...
你必须为你的类创建一个初始化器,它从你的 JSON 中获取一个字典作为参数来初始化具有正确类型的值。
例子:
class TestCast: NSObject
var ca:Int?
var resultat:Double?
var remuneration:Int?
var delta_ca:String?
var delta_resultat:String?
var delta_remuneration:String?
init(dictionary: [String:AnyObject])
self.ca = dictionary["ca"] as? Int
self.resultat = dictionary["resultat"] as? Double
self.delta_resultat = dictionary["delta_resultat"] as? String
self.delta_ca = dictionary["delta_ca"] as? String
self.remuneration = dictionary["remuneration"] as? Int
self.delta_remuneration = dictionary["delta_remuneration"] as? String
do
if let unwrappedData = data, let json = try NSJSONSerialization.JSONObjectWithData(unwrappedData, options: []) as? [String:[String:AnyObject]]
let casts = json.map TestCast(dictionary: $1)
// here `casts` is an array of TestCast objects
catch let error as NSError
print(error.description)
【讨论】:
以上是关于将 JSON 转换为自定义对象时出错的主要内容,如果未能解决你的问题,请参考以下文章
将 pandas 数据框转换为自定义 JSON 格式(然后转换为 JS 对象)
Azure APIM:将 JSON 响应转换为自定义 XML 格式