NSJSONSerialization 之 NSJSONReadingOptions 使用场景

使用Json 解析,准备放弃第三方解析库 ,打算转 系统的,看了看文档 ,对于这个参数的使用,通过验证没有发现 可见的区别,但是肯定有区别,应该是验证不全面,或者方式有问题。所以看看大家 在使用系统解析时 这个参数的场景 和 注意事项。分享探究…

官方文档
NSJSONReadingOptions

Options used when creating Foundation objects from JSON data—see JSONObjectWithData:options:error: and JSONObjectWithStream:options:error:.
enum {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
};
typedef NSUInteger NSJSONReadingOptions;
Constants
NSJSONReadingMutableContainers
Specifies that arrays and dictionaries are created as mutable objects.
Available in iOS 5.0 and later.
Declared in NSJSONSerialization.h.
NSJSONReadingMutableLeaves
Specifies that leaf strings in the JSON object graph are created as instances of NSMutableString.
Available in iOS 5.0 and later.
Declared in NSJSONSerialization.h.
NSJSONReadingAllowFragments
Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary.
Available in iOS 5.0 and later.
Declared in NSJSONSerialization.h.


在实际应用中,具体选择,三种同时验证:
id arrMutableContainers = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@“arrMutableContainers %@”,arrMutableContainers);
id arrAllowFragments = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@“arrAllowFragments %@”,arrAllowFragments);
id arrMutableLeaves = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSLog(@“arrMutableLeaves %@”,arrMutableLeaves);

NSLog:

arrMutableContainers (
{
id = 7;
img = “d/file/admin/2014/03/5323b482c465d.jpg”;
pid = 33;
},
{
id = 6;
img = “d/file/admin/2014/03/5323a647b397c.png”;
pid = 21;
}
)
2014-03-15 11:45:17.419 Custom[36850:60b] arrAllowFragments (
{
id = 7;
img = “d/file/admin/2014/03/5323b482c465d.jpg”;
pid = 33;
},
{
id = 6;
img = “d/file/admin/2014/03/5323a647b397c.png”;
pid = 21;
}
)
2014-03-15 11:45:17.420 Custom[36850:60b] arrMutableLeaves (
{
id = 7;
img = “d/file/admin/2014/03/5323b482c465d.jpg”;
pid = 33;
},
{
id = 6;
img = “d/file/admin/2014/03/5323a647b397c.png”;
pid = 21;
}
)

结果:【不明…】

选择与注意 大神们 有木有见解?

你是在教大家怎么用呢?还是问大家怎么用啊?

最好是介绍一些这个帖子的目的,背景,来龙去脉等等,方便大家快速带入

使用Json 解析,准备放弃第三方解析库 ,打算转 系统的,看了看文档 ,对于这个参数的使用,通过验证没有发现 可见的区别,但是肯定有区别,应该是验证不全面,或者方式有问题。所以看看大家 在使用系统解析时 这个参数的场景 和 注意事项。分享探究…

文档写的很清楚啊
NSJSONReadingMutableContainers,返回的json中的Dictionary和array都是mutable的。
NSJSONReadingMutableLeaves,返回的json如果叶子节点是string,就给你返回mutableString。
NSJSONReadingAllowFragments,允许解析的json顶层元素可以不是array和Dictionary。