现在的位置: 首页 > 综合 > 正文

JSON数据获取的三种方式

2018年01月23日 ⁄ 综合 ⁄ 共 2868字 ⁄ 字号 评论关闭

1.使用TouchJson

//获取API接口

    NSURL *url = [NSURLURLWithString:@"http://m.weather.com.cn/data/101010100.html"];

    //定义一个NSError对象,用于捕获错误信息

    NSError *error;

    NSString *jsonString = [NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];

    NSLog(@"jsonString--->%@",jsonString);

    //将解析得到的内容存放字典中,编码格式为UTF8,防止取值的时候发生乱码

    NSDictionary *rootDic = [[CJSONDeserializerdeserializer]deserialize:[jsonStringdataUsingEncoding:NSUTF8StringEncoding]error:&error];

    //因为返回的Json文件有两层,去第二层内容放到字典中去

    NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];

    NSLog(@"weatherInfo--->%@",weatherInfo);

    //取值打印

    txtView.text = [NSStringstringWithFormat:@"今天是 %@  %@  %@ 的天气状况是:%@  %@ ",[weatherInfo
objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];

2.使用SBJSON

NSURL *url = [NSURLURLWithString:@"http://m.weather.com.cn/data/101010100.html"];

    NSError *error = nil;

    NSString *jsonString = [NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];

    SBJsonParser *parser = [[SBJsonParseralloc] init];

    

    NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];

    NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];

    NSLog(@"%@", [weatherInfoallValues]);

    txtView.text = [NSStringstringWithFormat:@"今天是 %@  %@  %@ 的天气状况是:%@  %@ ",[weatherInfo
objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];

3.使用系统自带JSON解析

+ (void)netWorkWithURL:(NSString *)urlStr completion:(void (^)(id))block

{

    NSString *str = [urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    

    

    NSURL *url = [NSURLURLWithString:str];

    

    NSMutableURLRequest *request = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:60];

    

    request.HTTPMethod = @"get";

    

    [NSURLConnectionsendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response,NSData *data, NSError *connectionError) {

        

        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)lastObject];

        

        NSInteger hash = [urlStr hash];

        

        NSString *filePath = [cachesPathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%d.aaa", hash]];

        

        

        NSError *error = nil;

        if (data != nil) {

            id result = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:&error];

            block(result);

            

            BOOL re = [NSKeyedArchiverarchiveRootObject:data toFile:filePath];

            

            NSLog(@"%d", re);

        } else {

            NSData *dataRead = [NSKeyedUnarchiverunarchiveObjectWithFile:filePath];

            id result = [NSJSONSerializationJSONObjectWithData:dataRead options:NSJSONReadingMutableContainerserror:nil];

            

            block(result);

        }

        

        

    }];

}

【上篇】
【下篇】

抱歉!评论已关闭.