Objective-C JESON解析

以下是ios中三种不同解析方式:

 

jsonkit需要导入JSONKit.h、JSONKit.m文件,可在官网上下https://github.com/johnezang/JSONKit。

  1、JSONKit解析方式:

 

OC代码   收藏代码
  1.  NSString *jsonString = @"[{\"age\":18,\"book\":{\"price\":23.2,\"title\":\"booooooook1\"},\"name\":\"samyou\"},{\"age\":22,\"book\":{\"price\":21,\"title\":\"booooooook2\"},\"name\":\"samsam\"}]";  
  2.   
  3.     NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];  
  4. //    NSDictionary *resultDic = [jsonData objectFromJSONData];  
  5.     NSArray *resultDic = [jsonData objectFromJSONData];  
  6.     NSString *nstr = [resultDic JSONString];//json字符串  
  7.     NSLog(@"str : %@",nstr);  
  8.     NSLog(@"age= %@", [resultDic valueForKey:@"age"]);   
  9.     NSLog(@"book= %@", [resultDic valueForKey:@"book"]);  
  10.     NSArray *books = [resultDic valueForKey:@"book"];  
  11.     NSLog(@"book.price===%@",[books valueForKey:@"price"]);  

 

 

 

2、SBJSON解析方式(通过plist文件进行读取):

https://github.com/stig/json-framework可下载。

 

data.plist文件内容:[{"auctionId":1000,"auctionName":"苹果"},{"auctionId":1001,"auctionName":"李子"}]

OC代码   收藏代码
  1.  //json解析  
  2.     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];  
  3.     //获取字典  
  4.     NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:filePath];  
  5.     //获取json key值  
  6.     NSString *jsonData = [dataDict objectForKey:@"auction"];  
  7.     if(jsonData == nil){  
  8.         NSLog(@"无数据!");  
  9.     }else{  
  10.         //得到jsoin数组  
  11.         NSArray *jsonArray = [jsonData JSONValue];  
  12.         NSLog(@"jsonArray:%@",jsonArray);  
  13.         //通过key获取对应的值  
  14.         //写法一  
  15.         NSString *auctionId = [jsonArray valueForKey:AUCTION_ID];  
  16.         NSLog(@"auctionId : %@",auctionId);  
  17.         //写法二  
  18. //        NSArray *auctionId = [jsonArray valueForKey:AUCTION_ID];  
  19. //        for(int i=0;i<[auctionId count];i++){  
  20. //            NSLog(@"auction : %@",[auctionId objectAtIndex:i]);  
  21. //        }  
  22.         //写法三(以下方法不行)  
  23. //        for(int i=0;i<[jsonArray count];i++){  
  24. //            Auction *auction = [jsonArray objectAtIndex:i];  
  25. //            NSLog(@"Auction.auctionName : %@",auction.auctionName);  
  26. //        }  
  27.     
  28.     }  
  29.     [jsonData release];  

 

3、ios5自带API进行json解析,NSJSONSerialization类

OC代码   收藏代码
  1. NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];  
  2.         [dictionary setValue:@"Anthony" forKey:@"name"];  
  3.         [dictionary setValue:[NSNumber numberWithUnsignedInteger:30] forKey:@"Age"];  
  4.         NSArray *arrayChildren = [[NSArray alloc] initWithObjects:@"A", @"B",  nil];  
  5.         [dictionary setValue:arrayChildren forKey:@"children"];  
  6.         NSError *error = nil;  
  7.         NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];  
  8.         NSLog(@"jsonData : %@",[jsonData description]);  
  9.         if (error) {   
  10.             NSLog(@"dic->%@",error);  
  11.         }  
  12.         id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];  
  13.         if (nil != jsonObject) {  
  14.             if ([jsonObject isKindOfClass:[NSDictionary class]]){  
  15.                 NSDictionary *resultDic = (NSDictionary *)jsonObject;  
  16.                 NSLog(@"Received JSON Dictionary : %@", resultDic);  
  17.             } else {  
  18.                 NSLog(@"Error JSON data.");  
  19.             }  
  20.         }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值