invalid type in json write (__nsdate)

/* Generate JSON data from a Foundation object. If the object will not produce valid JSON then an exception will be thrown. Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated. If an error occurs, the error parameter will be set and the return value will be nil. The resulting data is a encoded in UTF-8.
*/
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:accoutInfoDict options:0 error:nil];

invalid type in json write (__nsdate)
NSDate --> expirationDate = "2017-06-04 19:00:00 +0000";
{
expirationDate = "2017-06-04 19:00:00 +0000";
largeAvatar = "https://tva3.sinaaa8.jpg";
midAvatar = "xxx";
openId = xxx;
sex = xxx;
sgid = "xxx";
tinyAvatar = "xxx";
token = "xxx";
uniqName = xxx;
userId = "xxx";
}

解决方案 使用NSKeyedArchiver 压缩dict 得到nsdata
NSData * jsonData = [NSKeyedArchiver archivedDataWithRootObject:accoutInfoDict];
/*
An NSData object containing the encoded form of the object graph whose root object is rootObject. The format of the archive is NSPropertyListBinaryFormat_v1_0.
*/
解析data 时需要用NSKeyedUnarchiver 反序列化 解析data 得到dict
NSDictionary *dictAccount = [NSKeyedUnarchiver unarchiveObjectWithData:accountData];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容