[C/OC的那点事儿]使用JSONKit进行JSON文件和NSString,NSArray,Dic NSData文件的相互转化

本文详细介绍了如何使用JSONKit进行JSON数据的解析与编码,包括将JSON字符串转化为字典,字典转化为JSON字符串,以及如何处理复杂的数据结构如嵌套字典和数组。通过实例演示了JSON数据在iOS应用开发中的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简单的JSONKit 包下的 转换

首先我们需要引入JSONKit.h,m文件.百度一下就能下载了.

//假如 str就是网络获取的json文件
        NSString *str = [NSString stringWithFormat:@"{\"id\":1,\"age\":\"2\"}"];
        NSDictionary *resultsDictionary = [str objectFromJSONString];//转化为字典类型的文件
        NSLog(@"%@",resultsDictionary);
        // insert code here...
        NSLog(@"Hello, World!");
        
        //dic文件转化为json文件
                  NSMutableDictionary *people = [NSMutableDictionary dictionary];
                    NSMutableDictionary *boy = [NSMutableDictionary dictionary];
                    NSMutableDictionary *girl= [NSMutableDictionary dictionary];
        
        
                  NSMutableDictionary *cat = [NSMutableDictionary dictionary];
                    NSMutableDictionary *pandon = [NSMutableDictionary dictionary];
        
        
               NSMutableDictionary *mammalia = [NSMutableDictionary dictionary];//哺乳类
        
              NSMutableDictionary *repilia = [NSMutableDictionary dictionary];//爬行类
        
            NSMutableDictionary *animal = [NSMutableDictionary dictionary];//
        
        [boy setObject:@"李蝉" forKey:@"lc"];
        [boy setObject:@"赵明伟" forKey:@"zz"];
        
        [girl setObject:@"李若" forKey:@"lr"];
        [girl setObject:@"徐娜" forKey:@"xn"];
        
        [people setObject:boy forKey:@"boy"];
        [people setObject:girl forKey:@"girl"];
        
        [pandon setObject:@"大熊猫" forKey:@"大pandon"];
        [pandon setObject:@"小猫" forKey:@"小pandon"];
        
        [cat setObject:pandon forKey:@"cat"];
        
        [mammalia setObject:people forKey:@"people"];
        [mammalia setObject:pandon forKey:@"pandon"];
        
        [animal setObject:mammalia forKey:@"animal"];
        
        
        
        [repilia setObject:@"pa虫" forKey:@"repilia"];
        
        [animal setObject:repilia forKey:@"爬行类"];
        
        NSString *string = [animal JSONString];
        NSLog(@"%@",string);

2013-11-29 11:34:51.692 JSON解析[1431:303] {

    age = 2;

    id = 1;

}

2013-11-29 11:34:51.694 JSON解析[1431:303] Hello, World!

2013-11-29 11:34:51.697 JSON解析[1431:303] {"爬行类":{"repilia":"pa"},"animal":{"pandon":{"pandon":"大熊猫","pandon":"小猫"},"people":{"boy":{"lc":"李蝉","zz":"赵明伟"},"girl":{"lr":"李若","xn":"徐娜"}}}}

Program ended with exit code: 0


  
        /*
         * json格式编码
         */
         NSString *res = nil;
        //字符串
        NSString *str = @"this is a nsstring";
        res = [str JSONString];
        NSLog(@"res= %@", [NSString stringWithString: res]);
        //res= "this is a nsstring"
        
        
        //数组
        NSArray *arr = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",nil];
        res = [arr JSONString];
        NSLog(@"res= %@", [NSString stringWithString: res]);
        [arr release];
        //res= ["One","Two","Three"]
        
        
        //字典类型(对象)
        NSArray *arr1 = [NSArray arrayWithObjects:@"dog",@"cat",nil];
        NSArray *arr2 = [NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithInt:30],nil];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
        res = [dic JSONString];
        NSLog(@"res= %@", [NSString stringWithString: res]);
        //res= {"pets":["dog","cat"],"other":[true,30]}
        
        
        /*
         * json格式解码
         */
        JSONDecoder *jd=[[JSONDecoder alloc] init];
        
        //针对NSData数据
        NSData *data = [dic JSONData];
        NSDictionary *ret = [jd objectWithData: data];
        NSLog(@"res= %@", [ret objectForKey:@"pets"]);
        //res= (
        //	dog,
        //	cat
        //)
        NSLog(@"res= %@", [[ret objectForKey:@"other"] objectAtIndex:0]);
        //res= 1
        
        //针对NSString字符串数据
        NSString *nstr = [dic JSONString];
        NSDictionary *ret2 = [jd objectWithUTF8String:(const unsigned char *)[nstr UTF8String] length:(unsigned int)[nstr length]];
        NSLog(@"res= %d", [[ret2 objectForKey:@"pets"] indexOfObject:@"cat"]);
        //res= 1
        NSLog(@"res= %@", [[ret2 objectForKey:@"other"] objectAtIndex:1]);
        //res= 30


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值