码迷,mamicode.com
首页 > 移动开发 > 详细

iOS网络: 把Array和Dictionaries序列化成JSON对象

时间:2014-11-11 18:54:51      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   使用   sp   for   

你想把 Array 和 dictionary 序列化成 JSON 对象。以方便在网络中传输或者保存到磁盘中。 

方案:

使用 NSJSONSerialization 这个类的 dataWithJSONObject:options:error:方法来实现。 

讨论:

NSJSONSerialization 这个类的 dataWithJSONObject:options:error:方法可以对数组和字典进行序列化,这些数组和字典包含的值为:NSString、NSNumber、NSArray、NSDictionary 和代表 nil 值的 NSNull。传递到这个方法中的参数要么是数组要么是字典。 
 

例子:

下面我们来创建一个简单的数组:包含一些键值:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
    @"FirstName":@"Anthony",
    @"Last Name" : @"Robbins",
    @"Age" : @51
    }];
    NSArray *arrayOfAnthonysChildren = @[
                                         @"Anthony‘s Son 1",
                                         @"Anthony‘s Daughter 1",
                                         @"Anthony‘s Son 2",
                                         @"Anthony‘s Son 3",
                                         @"Anthony‘s Daughter 2"
                                         ];
    [dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];

将字典序列化为JSON对象

 

iOS网络: 把Array和Dictionaries序列化成JSON对象

标签:style   blog   io   color   ar   os   使用   sp   for   

原文地址:http://www.cnblogs.com/safiri/p/4090108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!