私人资料库
本博客大部分技术文章,均从网络搜索得来,旨在收集整理技术资料,文章版权归属原作者,由此引起的任何版权问题,与本人无关。

转自:iPhone上的JSON

 

JSON我就不多解释了,需要更多信息的朋友请到json.org上查看。

在iPhone上访问网络内容是很必须的,而一些数据就需要以某种形式储存在web服务器上。比如一个app的目录,内容,索引等等。而xml和json,plist都是比较方便的方式。

-XML在iPhone上是非常好用的,但是对复杂的数据结构使用上就不那么方便了,具体可以参阅苹果的“基于事件的XML”和“基于树的XML”编程向导

-plist是再方便不过了,不过我看最多也就是一个NSDictionary而已,复杂一些的话,数据输入上也会非常非常的麻烦。

-JSON本来是不被苹果支持的,但是有人很Nice的帮我们解决了这个问题:JSON for OBJC http://code.google.com/p/json-framework/

基本上来说,这个框架异常的简单易用,会将得到的json字符串处理成一个复杂NSDictionary对象,而每一个值都还是一个NSDictionary对象

比如:

 1 {
 2     "华藏净宗学会":
 3     {
 4         "zhaomu":
 5         {
 6             "name":"净宗朝暮课本",
 7             "length":142,
 8             "digits":3
 9         },
10         "kesong":
11         {
12             "name":"净宗共修课本",
13             "length":75,
14             "digits":2
15         }
16     },
17     "生命基金会":
18     {
19         "dabei88":
20         {
21             "name":"大悲出相图",
22             "length":88,
23             "digits":2
24         }
25     }
26 }


就会转换为一个复杂无比的NSDictionary:

 1 [[NSDictionary alloc] 
 2  initWithObjects:[NSArray 
 3           arrayWithObjects:
 4           [NSDictionary 
 5            dictionaryWithObjects:[NSArray 
 6                   arrayWithObjects:
 7                   [NSDictionary 
 8                    dictionaryWithObjects:[NSArray 
 9                           arrayWithObjects:
10                               @"净宗朝暮课本",
11                               @"142",
12                               @"3",nil] 
13                    forKeys:
14                    [NSArray arrayWithObjects:
15                    @"name",
16                    @"length",
17                    @"digits",nil]],
18                   [NSDictionary 
19                    dictionaryWithObjects:[NSArray 
20                           arrayWithObjects:
21                           @"净宗共修课本",
22                           @"75",
23                           @"2",nil] 
24                    forKeys:
25                    [NSArray arrayWithObjects:
26                    @"name",
27                    @"length",
28                    @"digits",nil]],nil]
29            forKeys:[NSArray arrayWithObjects:@"zhaomu",@"kesong",nil]],
30           [NSDictionary 
31            dictionaryWithObjects:[NSArray 
32                   arrayWithObjects:
33                   [NSDictionary 
34                    dictionaryWithObjects:[NSArray 
35                           arrayWithObjects:
36                           @"大悲出相图",
37                           @"88",
38                           @"2",nil] 
39                    forKeys:
40                    [NSArray arrayWithObjects:
41                    @"name",
42                    @"length",
43                    @"digits",nil]],nil]
44            forKeys:[NSArray arrayWithObjects:@"dabei88",nil]],nil]
45  forKeys:[NSArray arrayWithObjects:@"华藏净宗学会",@"生命基金会",nil]];


我是非常佩服自己能打出来上面的巨大无比的定义式。。。。没有编译错误

不管怎么样,转换后,在系统中就可以非常方便的使用json的键值结构信息咯~!!!

 

posted on 2011-01-14 12:03  该显示名称已被其他用户使用  阅读(522)  评论(0)    收藏  举报