现在的位置: 首页 > 综合 > 正文

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

2013年01月25日 ⁄ 综合 ⁄ 共 4777字 ⁄ 字号 评论关闭

通过 NSJSONSerialization 这个类的
d
ataWithJSONObject: options: error:方法来实现。 
  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

  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

抱歉!评论已关闭.