`

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"]];
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics