ios 解析json文件 使用touchjson来解析

git hub 地址   https://github.com/TouchCode/TouchJSON

//
//  ViewController.m
//  JsonProject
//
//  Created by seandeng on 6/1/15.
//  Copyright (c) 2015 seandeng. All rights reserved.
//

#import "ViewController.h"
#import "CJSONDeserializer.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextView *txtView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取API接口
    NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
    //定义一个NSError对象,用于捕获错误信息
    NSError *error;
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"jsonString--->%@",jsonString);
    //将解析得到的内容存放字典中,编码格式为UTF8,防止取值的时候发生乱码
    NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    //因为返回的Json文件有两层,去第二层内容放到字典中去
    NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
    NSLog(@"weatherInfo--->%@",weatherInfo);
    //取值打印
    _txtView.text = [NSString stringWithFormat:@"今天是 %@  %@  %@  的天气状况是:%@  %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

猜你喜欢

转载自dsr-22.iteye.com/blog/2215967
今日推荐