JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

son和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子。

而不格式化的时候json和xml 又是一个普普通通的字符串,在网络通信的时候也只需要请求一次,而不用每次为得到木一个值而重复的请求服务器或者目标主机,

json和xml 都采用 键 - 值 的形式来存放数据。

xml 使用: <键> 值 </键>

json 使用:  "键" : "值"

苹果公司提供了一个官方的json解析库 NSJSONSerialization   

NSJSONSerialization  里面包含了两个方法来通过不同数据形式来解析json数据。

1、+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; //使用缓冲区数据来解析

2、+ (id)JSONObjectWithStream:(NSInputStream *)stream options:(NSJSONReadingOptions)opt error:(NSError **)error; // 使用文件流的形式来解析json

 

解析json的步骤大概是,先把json字符串读取到缓冲区,然后使用NSJSONSerialization    里面的方法进行解析,根据不同json 格式可能返回的数据类型不一样,所以最好用 id 类型接。

eg:  id dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];

在得到解析后的数据时,然后就一步一步 ,一个一个 键 - 值 的去寻找你想要的咚咚。

 

Eg 1 : 从本地文件中读取json数据,然后读取到缓冲区。


- (void)jsonParse{
   
    //初始化文件路径。
    NSString* path  = [[NSBundle mainBundle] pathForResource:@"nanjing" ofType:@"txt"];
   //将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,
    NSString* jsonString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
   //将字符串写到缓冲区。
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    //解析json数据,使用系统方法 JSONObjectWithData:  options: error:
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
    //接下来一步一步解析。知道得到你想要的东西。
    NSArray* arrayResult =[dic objectForKey:@"results"]; 
    NSDictionary* resultDic = [arrayResult objectAtIndex:0];
    NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
    NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);
    NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
    NSNumber* lat = [locationDic objectForKey:@"lat"];
    NSNumber* lng = [locationDic objectForKey:@"lng"];
    NSLog(@"lat = %@, lng = %@",lat,lng);
    [jsonString release];
    
    
}

 

 

Eg 2 :使用网络路径来解析json,

- (void)jsonParse{
   
    //初始化网络路径。
    NSString* path  = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
    //初始化 url 
    NSURL* url = [NSURL URLWithString:path];
   //将文件内容读取到字符串中,注意编码NSUTF8StringEncoding 防止乱码,
    NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
   //将字符串写到缓冲区。
    NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    //解析json数据,使用系统方法 JSONObjectWithData:  options: error:
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
    
    //一下为自定义解析, 自己想怎么干就怎么干
    
    NSArray* arrayResult =[dic objectForKey:@"results"]; 
    NSDictionary* resultDic = [arrayResult objectAtIndex:0];
    NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
    NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);
    NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
    NSNumber* lat = [locationDic objectForKey:@"lat"];
    NSNumber* lng = [locationDic objectForKey:@"lng"];
    NSLog(@"lat = %@, lng = %@",lat,lng);
    [jsonString release];
        
}

 


Eg 3 :使用网络路径来解析json 。 使用NSURLRequest 和NSURLConnection 请求网络数据。

- (void)jsonParse{
   
    //初始化网络路径。
    NSString* path  = @"http://maps.googleapis.com/maps/api/geocode/json?address=nanjing&sensor=true";
    //初始化 url 
    NSURL* url = [NSURL URLWithString:path];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    //将请求到的字符串写到缓冲区。
    NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //解析json数据,使用系统方法 JSONObjectWithData:  options: error:
    NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
    
    //一下为自定义解析, 自己想怎么干就怎么干
    
    NSArray* arrayResult =[dic objectForKey:@"results"]; 
    NSDictionary* resultDic = [arrayResult objectAtIndex:0];
    NSDictionary* geometryDic = [resultDic objectForKey:@"geometry"];
    NSLog(@"geometryDic: %@,  resultDic:%@",geometryDic,resultDic);
    NSDictionary* locationDic = [geometryDic objectForKey:@"location"];
    NSNumber* lat = [locationDic objectForKey:@"lat"];
    NSNumber* lng = [locationDic objectForKey:@"lng"];
    NSLog(@"lat = %@, lng = %@",lat,lng);
        
}

demo下载 :http://download.csdn.net/download/wsq724439564/6207829

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值