Ios NSJSONSerialization将结果NSDictionary搞乱
我在解析web服务的json输出时遇到问题。我正在使用NSJSONSerialization将输出解析到NSJSONDictionary中。还可以通过子类化AFHTTPSessionManager来使用AFNetworking。目前,响应序列化器是返回NSData的AFHTTPResponseSerializer 以下是我使用的代码:Ios NSJSONSerialization将结果NSDictionary搞乱,ios,ios7,nsdictionary,afnetworking-2,nsjsonserialization,Ios,Ios7,Nsdictionary,Afnetworking 2,Nsjsonserialization,我在解析web服务的json输出时遇到问题。我正在使用NSJSONSerialization将输出解析到NSJSONDictionary中。还可以通过子类化AFHTTPSessionManager来使用AFNetworking。目前,响应序列化器是返回NSData的AFHTTPResponseSerializer 以下是我使用的代码: NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject optio
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&err];
很简单。并且err对象为nil,因此转换工作正常
但是:我直接从web服务得到的结果是:
"address":
{
"address1": "Ivy House",
"address2": "Sandy Lane",
"city": "Rush",
"postCode": null,
"email": "notknown@whatever.com",
"telephone": "18437584",
"mobile": null,
"smsAlert": null,
"county": "Dublin",
"country": "Ireland",
"websiteAddress": "www.example.com"
},
我在打印dict的内容后得到的结果是:
address = {
address1 = "Ivy House";
address2 = "Sandy Lane";
city = Rush;
country = Ireland;
county = Dublin;
email = "notknown@whatever.com";
mobile = "<null>";
postCode = "<null>";
smsAlert = "<null>";
telephone = 18437584;
websiteAddress = "www.example.com";
};
地址={
地址1=“常春藤之家”;
地址2=“沙巷”;
城市=匆忙;
国家=爱尔兰;
郡=都柏林;
电子邮件=”notknown@whatever.com";
mobile=“”;
邮政编码=”;
smsAlert=“”;
电话=18437584;
websiteAddress=“www.example.com”;
};
问题是生成的NSDictionary没有双引号,因此以plist格式将NSDictionary保存到磁盘失败
我也尝试过使用AFJSONResponseSerializer,它返回NSDictionary,但内容与上面相同
问题在哪里
提前感谢。NSDictionary的结构与JSON字符串不同。因此,当您将JSON字符串解析为NSDictionary时,不应使用引号 您可以保存JSON数据并在运行时再次解析它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];
// Save it into file system
[data writeToFile:dataPath atomically:YES];
NSDictionary的结构与JSON字符串不同。因此,当您将JSON字符串解析为NSDictionary时,不应使用引号 您可以保存JSON数据并在运行时再次解析它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];
// Save it into file system
[data writeToFile:dataPath atomically:YES];
NSDictionary的结构与JSON字符串不同。因此,当您将JSON字符串解析为NSDictionary时,不应使用引号 您可以保存JSON数据并在运行时再次解析它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];
// Save it into file system
[data writeToFile:dataPath atomically:YES];
NSDictionary的结构与JSON字符串不同。因此,当您将JSON字符串解析为NSDictionary时,不应使用引号 您可以保存JSON数据并在运行时再次解析它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];
// Save it into file system
[data writeToFile:dataPath atomically:YES];
这里的问题似乎与双引号无关。当打印时,<代码> nSc字典< /COD>(以及其他基础对象)在不需要的时候(例如,在字符串中没有空格或特殊字符)时,关闭双引号。 现在,阻止您将
NSDictionary
序列化到属性列表的可能问题是JSON中存在null
,因此在NSDictionary
中也存在。根据:
属性列表对象包括NSData
、NSString
、NSArray
、NSDictionary
、NSDate
和NSNumber
对象
然而,JSON中的
null
将表示为NSNull
的实例,从而使您的NSDictionary
成为无效的属性列表。这里的问题似乎与双引号无关。当打印时,<代码> nSc字典< /COD>(以及其他基础对象)在不需要的时候(例如,在字符串中没有空格或特殊字符)时,关闭双引号。
现在,阻止您将NSDictionary
序列化到属性列表的可能问题是JSON中存在null
,因此在NSDictionary
中也存在。根据:
属性列表对象包括NSData
、NSString
、NSArray
、NSDictionary
、NSDate
和NSNumber
对象
然而,JSON中的
null
将表示为NSNull
的实例,从而使您的NSDictionary
成为无效的属性列表。这里的问题似乎与双引号无关。当打印时,<代码> nSc字典< /COD>(以及其他基础对象)在不需要的时候(例如,在字符串中没有空格或特殊字符)时,关闭双引号。
现在,阻止您将NSDictionary
序列化到属性列表的可能问题是JSON中存在null
,因此在NSDictionary
中也存在。根据:
属性列表对象包括NSData
、NSString
、NSArray
、NSDictionary
、NSDate
和NSNumber
对象
然而,JSON中的
null
将表示为NSNull
的实例,从而使您的NSDictionary
成为无效的属性列表。这里的问题似乎与双引号无关。当打印时,<代码> nSc字典< /COD>(以及其他基础对象)在不需要的时候(例如,在字符串中没有空格或特殊字符)时,关闭双引号。
现在,阻止您将NSDictionary
序列化到属性列表的可能问题是JSON中存在null
,因此在NSDictionary
中也存在。根据:
属性列表对象包括NSData
、NSString
、NSArray
、NSDictionary
、NSDate
和NSNumber
对象
然而,JSON中的
null
将表示为NSNull
的实例,从而使您的NSDictionary
成为无效的属性列表。示例中缺少双引号只是因为这是NSDictionary(甚至可能是NSString)的打印方式。如果将此词典保存到属性列表失败,则是由于另一个原因(您可以使用[NSPropertyListSerialization dataWithPropertyList:format:options:error:
中的错误进行跟踪)。我同意@Andrey的观点。NSDictionary description似乎没有为仅包含字母数字字符的字符串添加引号(没有分隔符)。@Andrey我无法保存到plist,因此无法再次尝试阅读