JSON解析

JSON解析
1. 利用JSONKit第三方库解析json文件
1) 下载JSONKit  https://github.com/johnezang/JSONKit
2) 将JSONKit.h和JSONKit.m copy到我们应用的工程中
3) 注意是不是ARC
       

- (void)parseUseJSONKit
      {
   NSURL *url = [NSURL 
URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *weatherDic = [jsonString objectFromJSONString];
    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
    m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]];
      }

 
2. 利用SBJson第三方库解析json文件
1) 下载SBJson  https://github.com/stig/json-framework
2) 编译下载号的工程,将静态库libsbjson-ios.a文件导入到我们应用的工程中,并且将相应的头文件导入到应用中(其中包括: SBJson.h,SBJsonParser.h, SBJsonWriter.h, SBJsonStreamParser.h, SBJsonStreamParserAdapter.h, SBJsonStreamWriter.h, SBJsonStreamTokeniser.h, SBJsonStreamWriter.h, SBJsonStreamParserAdapter.h)
3) 进入” SBJson.h”,这个文件是支持ARC的
4) 使用过程如下:

- (void)parseUseSBJson
{
    NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    SBJsonParser *parser = [[SBJsonParser alloc]init];
    
    NSDictionary *rootDic = [parser objectWithString:jsonString];
   NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
    m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]];
}

 

3. iOS SDK自动带的NSJSONSerialization类解析json文件
直接使用NSJSONSerialization类,如下:

- (void)parseUseNSJSONSerialization
{
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:nil];
    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
   m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]];
}

 
4. 利用TouchJson第三方库解析json文件
1) 下载TouchJson包https://github.com/TouchCode/TouchJSON
2) 解压后将source文件夹中的类文件导入到我们应用的工程中
3) 注意:类库用额是非ARC
4) 引入CJSONDeserializer.h
5) 应用如下:

- (void)parseUseTouchJson
{
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
  NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSDictionary *rootDic = [[CJSONDeserializer deserializer]deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:nil];
    NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
    m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]];
}

 
5. 利用NextiveJson第三方库解析json文件
1) 下载NextiveJson第三方库 https://github.com/nextive/NextiveJson
2) 应用工程设置为非ARC
3) 将Extensions.h, Extensions.m, NXDebug.h, NXDebug.m, NXJsonParser.h, NXJsonParser.m, NXJsonSerializer.h, NXJsonSerializer.m, NXSerializable.h 文件copy到工程中
4) 使用NextiveJson,解析json文件,如下:

- (void)parseUseNextiveJson
{
    NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *weatherDic = [NXJsonParser parseData:responseData error:nil ignoreNulls:NO];
    NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
    m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]];
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值