IOS学习 JSON与Arrays 或者 Dictionaries相互转换


通过  NSJSONSerialization  这个类的 d ataWithJSONObject: options: error: 方法来实现。 
[html]  view plain copy print ?
  1. //dictionary序列化成json  
  2.    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];  
  3.    [dictionary setValue:@"Anthony"forKey:@"First Name"];  
  4.    [dictionary setValue:@"Robbins"forKey:@"Last Name"];  
  5.    [dictionary setValue:[NSNumber numberWithUnsignedInteger:51]forKey:@"Age"];  
  6.    NSArray *arrayOfAnthonysChildren = [[NSArray alloc]  
  7.                                        initWithObjects:  
  8.                                        @"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2",nil];  
  9.    [dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];  
  10.    NSError *error = nil;  
  11.    //序列化数据成json的data。。。。。。。。。。。。。。。。。。。。。。。  
  12.    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary  
  13.                                                       options:NSJSONWritingPrettyPrinted  
  14.                                                         error:&error];  
  15.    if ([jsonData length] > 0 && error == nil){  
  16.        NSLog(@"已把字典成功序列化.");  
  17.        //把json数据转化为String类型  
  18.        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
  19.        NSLog(@"JSON String = %@", jsonString);  
  20.          
  21.     //把 JSON 数据转化成 Arrays 或者 Dictionaries      
  22.    //反序列化。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。  
  23.        id jsonObject = [NSJSONSerialization  
  24.                         JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments  
  25.                         error:&error];  
  26.        if (jsonObject != nil && error == nil){  
  27.            NSLog(@"反序列化成功...");  
  28.            if ([jsonObject isKindOfClass:[NSDictionary class]]){  
  29.                NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;  
  30.                NSLog(@"反序列化后的dictionary数据 = %@", deserializedDictionary);  
  31.            }  
  32.            else if ([jsonObject isKindOfClass:[NSArray class]]){  
  33.                NSArray *deserializedArray = (NSArray *)jsonObject;  
  34.                NSLog(@"反序列化json后的数组 = %@", deserializedArray);  
  35.            }else {  
  36.                  
  37.            }  
  38.          
  39.        }else if (error != nil){  
  40.            NSLog(@"反序列化时发生一个错误");  
  41.        }  
  42.          
  43.    } else if ([jsonData length] == 0 && error == nil){  
  44.       NSLog(@"序列化后没有返回数据");  
  45.    }else if (error != nil){  
  46.      NSLog(@"错误: %@", error);  
  47.    }  
  48.      

本文转自:点击打开链接http://blog.csdn.net/wildcatlele

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值