NSJSONSerialization JSONObjectWithData options的解析转论坛

原文地址:http://www.cocoachina.com/bbs/read.PHP?tid=110907

我google NSJSONReadingOptions,这帖子出现在第一,但是楼上各位都没把问题说明白。


首先用代码来说明NSJSONReadingMutableContainers的作用:
复制代码
  1. Nsstring *str = @"{\"name\":\"kaixuan_166\"}";
  2. NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
  3. // 应用崩溃,不选用NSJSONReadingOptions,则返回的对象是不可变的,NSDictionary
  4. [dict setobject:@"male" forKey:@"sex"];
  5. NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
  6. // 没问题,使用NSJSONReadingMutableContainers,则返回的对象是可变的,NSMutableDictionary
  7. NSLog(@"%@",dict);
NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NSMutableArray。 NSJSONReadingMutableLeaves:返回的JSON对象中字符串的值为NSMutableString,目前在iOS 7上测试不好用,应该是个bug,参见: http://stackoverflow.com/questions/19345864/nsjsonreadingmutableleaves-option-is-not-working NSJSONReadingallowFragments:允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。参见: http://stackoverflow.com/questions/16961025/nsjsonserialization-nsjsonreadingallowfragments-reading

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...