天天看點

天氣預報---網絡加載最新天氣資訊,截取JSON檔案仿普通的手機上的天氣預報功能,從網絡上截取JSON檔案

仿普通的手機上的天氣預報功能,從網絡上截取JSON檔案

實作的效果圖如下:

天氣預報---網絡加載最新天氣資訊,截取JSON檔案仿普通的手機上的天氣預報功能,從網絡上截取JSON檔案

具體代碼如下:

FirstViewController.m檔案

#import "FirstViewController.h"

#import "JRViewController.h"

#import "CityModel.h"

#import "JSONKit.h"

#define kW self.view.frame.size.width

#define kH self.view.frame.size.height

@interface FirstViewController ()

//目前城市

@property (nonatomic,copy) NSString * cityName;

//目前日期

@property (nonatomic,strong) NSArray * dateArray;

//目前天氣小圖檔

@property (nonatomic,strong) NSArray * imageViewArray;

//目前溫度label

@property (nonatomic,strong) NSArray * temperArray;

//天氣詳情介紹

@property (nonatomic,strong) NSArray * introArray;

//右上角,目前溫度

@property (nonatomic,copy) NSString * rightTemper;

//右上角,天氣情況

@property (nonatomic,copy) NSString * rightIntro;

//曬衣指數

@property (nonatomic,copy) NSString * dress;

//紫外線指數

@property (nonatomic,copy) NSString * purple;

//洗車指數

@property (nonatomic,copy) NSString * car;

//人體舒适程度

@property (nonatomic,copy) NSString * comfort;

@end

@implementation FirstViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    UIImageView * imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"DuoYun"]];

    imageView.frame=self.view.bounds;

    [self.view addSubview:imageView];

    self.title=@"天氣預報";

    //加載煙台的天氣

    [self _loadYT];

    //加載視圖

    [self _loadView];   

}

#pragma mark - 加載視圖

- (void) _loadView

{

    //右上角當天溫度情況

    UILabel * leftLabel=[[UILabel alloc]initWithFrame:CGRectMake(200, 60, kW-200, 50)];

    //leftLabel.backgroundColor=[UIColor redColor];

    leftLabel.textAlignment=NSTextAlignmentCenter;

    leftLabel.text=_rightTemper;

    [leftLabel setFont:[UIFont fontWithName:nil size:30]];

    leftLabel.textColor=[UIColor whiteColor];

    [self.view addSubview:leftLabel];

    //右上角,天氣情況

    UILabel * rightIntroLabel=[[UILabel alloc]initWithFrame:CGRectMake(200, 100, kW-200, 40)];

    //rightIntroLabel.backgroundColor=[UIColor greenColor];

    rightIntroLabel.text=_rightIntro;

    [rightIntroLabel setTextColor:[UIColor blueColor]];

    rightIntroLabel.textAlignment=NSTextAlignmentCenter;

    [self.view addSubview:rightIntroLabel];

    //目前城市

    UILabel * RightCityLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 70, 150, 70)];

    //RightCityLabel.backgroundColor=[UIColor greenColor];

    [RightCityLabel setTextColor:[UIColor whiteColor]];

    RightCityLabel.textAlignment=NSTextAlignmentCenter;

    RightCityLabel.text=_cityName;

    [RightCityLabel setFont:[UIFont fontWithName:nil size:40]];

    [self.view addSubview:RightCityLabel];

    //切換城市按鈕

    UIButton * changeCityButton=[[UIButton alloc]initWithFrame:CGRectMake((kW-100)/2, kH-50, 100, 45)];

    changeCityButton.backgroundColor=[UIColor purpleColor];

    [changeCityButton setTitle:@"切換城市" forState:UIControlStateNormal];

    [changeCityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [changeCityButton addTarget:self action:@selector(changeCity) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:changeCityButton];

    //下方多日天氣視圖

    for (int i=0; i<4; i++)

    {

        UIView * view=[[UIView alloc]initWithFrame:CGRectMake(((kW-4*80)/5)*(i+1)+i*80, kH-290, 80, 230)];

        view.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];

        //date

        UILabel * dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];

        dateLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];

        dateLabel.text=_dateArray[i];

        dateLabel.textAlignment=NSTextAlignmentCenter;

        [view addSubview:dateLabel];

        //image

        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 55, 80, 80)];

        //imageView.backgroundColor=[UIColor greenColor];

        NSURL * url=[NSURL URLWithString:_imageViewArray[i]];

        NSData * data=[NSData dataWithContentsOfURL:url];

        UIImage * image=[UIImage imageWithData:data];

        imageView.image=image;

        [view addSubview:imageView];

        //天氣情況簡介label

        UILabel * intrLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 130, 80, 60)];

        //intrLabel.backgroundColor=[UIColor redColor];

        intrLabel.textAlignment=NSTextAlignmentCenter;

        intrLabel.adjustsFontSizeToFitWidth=YES;

        intrLabel.text=_introArray[i];

        [view addSubview:intrLabel];

        //溫度label

        UILabel * tempery=[[UILabel alloc]initWithFrame:CGRectMake(0, 190, 80, 40)];

        tempery.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];

        tempery.text=_temperArray[i];

        tempery.textAlignment=NSTextAlignmentCenter;

        [tempery setFont:[UIFont fontWithName:nil size:15]];

        [view addSubview:tempery];

        [self.view addSubview:view];

        //中間比較大的視圖

        UIView * middleView=[[UIView alloc]initWithFrame:CGRectMake(50, 140, kW-100, 225)];

        middleView.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.05];

        //曬衣指數

        UILabel * dressLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 40)];

        dressLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];

        dressLabel.text=@"曬衣指數";

        dressLabel.textAlignment=NSTextAlignmentCenter;

        dressLabel.textColor=[UIColor whiteColor];

        [middleView addSubview:dressLabel];

        UILabel * RdressLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 5, 250, 50)];

        //RdressLabel.backgroundColor=[UIColor redColor];

        RdressLabel.text=_dress;

        [RdressLabel setTextColor:[UIColor whiteColor]];

        [middleView addSubview:RdressLabel];

        //紫外線指數

        UILabel * purpleLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 65, 100,40)];

        purpleLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];

        purpleLabel.text=@"紫外線指數";

        purpleLabel.textAlignment=NSTextAlignmentCenter;

        purpleLabel.textColor=[UIColor whiteColor];

        [middleView addSubview: purpleLabel];

        UILabel * RpurpleLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 10+50, 250,50)];

        //RpurpleLabel.backgroundColor=[UIColor redColor];

        RpurpleLabel.text=_purple;

        [RpurpleLabel setTextColor:[UIColor whiteColor]];

        [middleView addSubview: RpurpleLabel];

        //洗車指數

        UILabel * carLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 65+40+15, 100, 40)];

        carLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];

        carLabel.text=@"洗車指數";

        carLabel.textAlignment=NSTextAlignmentCenter;

        carLabel.textColor=[UIColor whiteColor];

        [middleView addSubview:carLabel];

        UILabel * RcarLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 65+50, 250, 50)];

        //RcarLabel.backgroundColor=[UIColor redColor];

        RcarLabel.text=_car;

        [RcarLabel setTextColor:[UIColor whiteColor]];

        [middleView addSubview:RcarLabel];

        //人體舒适程度

        UILabel * personComfortLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 115+50+5, 100, 40)];

        personComfortLabel.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.06];

        personComfortLabel.text=@"人體舒适程度";

        personComfortLabel.adjustsFontSizeToFitWidth=YES;

        personComfortLabel.textAlignment=NSTextAlignmentCenter;

        personComfortLabel.textColor=[UIColor whiteColor];

        [middleView addSubview:personComfortLabel];

        UILabel * RpersonComfortLabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 115+50, 250, 50)];

        //RpersonComfortLabel.backgroundColor=[UIColor redColor];

        RpersonComfortLabel.text=_comfort;

        [RpersonComfortLabel setTextColor:[UIColor whiteColor]];

        [middleView addSubview:RpersonComfortLabel];

        [self.view addSubview:middleView];

    }

}

#pragma mark - 加載煙台的天氣

- (void) _loadYT

{

    NSURL * url=[NSURL URLWithString:@"http://m.weather.com.cn/atad/101120501.html"];

    NSData * data=[NSData dataWithContentsOfURL:url];

    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    NSDictionary * tempDic=dic[@"weatherinfo"];

    //城市名

    NSString * cityName=tempDic[@"city"];

    _cityName=cityName;

    //日期數組

    NSDate * date1=[NSDate date];

    NSDate * date2=[NSDate dateWithTimeIntervalSinceNow:60*60*24];

    NSDate * date3=[NSDate dateWithTimeIntervalSinceNow:60*60*24*2];

    NSDate * date4=[NSDate dateWithTimeIntervalSinceNow:60*60*24*3];

    NSDateFormatter * format=[[NSDateFormatter alloc]init];

    [format setDateFormat:@"MM月dd日"];

    NSString * str1=[format stringFromDate:date1];

    NSString * str2=[format stringFromDate:date2];

    NSString * str3=[format stringFromDate:date3];

    NSString * str4=[format stringFromDate:date4];

    _dateArray=@[str1,str2,str3,str4];

    //溫度數組

    NSString * temper1=tempDic[@"temp1"];

    NSString * temper2=tempDic[@"temp2"];

    NSString * temper3=tempDic[@"temp3"];

    NSString * temper4=tempDic[@"temp4"];

    _temperArray=@[temper1,temper2,temper3,temper4];

    //天氣小圖示數組---存放圖檔位址

    NSString * string1=tempDic[@"img1"];

    NSInteger index1=[string1 integerValue];

    NSString * imageSrc1=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

    imageSrc1=[NSString stringWithFormat:imageSrc1,index1];

    //NSLog(@"%@",imageSrc1);

    NSString * string2=tempDic[@"img3"];

    NSInteger index2=[string2 integerValue];

    NSString * imageSrc2=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

    imageSrc2=[NSString stringWithFormat:imageSrc2,index2];

    //NSLog(@"%@",imageSrc2);

    NSString * string3=tempDic[@"img5"];

    NSInteger index3=[string3 integerValue];

    NSString * imageSrc3=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

    imageSrc3=[NSString stringWithFormat:imageSrc3,index3];

    //NSLog(@"%@",imageSrc3);

    NSString * string4=tempDic[@"img7"];

    NSInteger index4=[string4 integerValue];

    NSString * imageSrc4=@"http://i.tq121.com.cn/i/mobile/images/d%02d.png";

    imageSrc4=[NSString stringWithFormat:imageSrc4,index4];

    //NSLog(@"%@",imageSrc4);

    _imageViewArray=@[imageSrc1,imageSrc2,imageSrc3,imageSrc4];

    //天氣詳情數組

    NSString * intr1=tempDic[@"weather1"];

    NSString * intr2=tempDic[@"weather2"];

    NSString * intr3=tempDic[@"weather3"];

    NSString * intr4=tempDic[@"weather4"];

    _introArray=@[intr1,intr2,intr3,intr4];

   //右上角當天的溫度情況

    _rightTemper=tempDic[@"temp1"];

    //右上角當天天氣情況

    _rightIntro=tempDic[@"weather1"];

    //晾衣指數

    _dress=tempDic[@"index_ls"];

    //紫外線指數

    _purple=tempDic[@"index_uv"];

    //洗車指數

    _car=tempDic[@"index_xc"];

    //人體舒适程度

    _comfort=tempDic[@"index_co"];

}

- (void) _loadData

{

    NSString * path=[[NSBundle mainBundle] pathForResource:@"weather_city_code.json" ofType:nil];

    NSData * data=[NSData dataWithContentsOfFile:path];

    NSDictionary * dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    //NSLog(@"%@",dic);

    NSArray * array=dic[@"城市代碼"];

    //NSLog(@"%@",array);

    //建立model

    CityModel * citymodel=[[CityModel alloc]init];

    for (int i=0; i<array.count; i++)

    {

        NSDictionary * tempDic=array[i];

        //NSLog(@"%@",tempDic);

        citymodel.provinceName=tempDic[@"省"];    //擷取到各省

        //NSLog(@"%@",citymodel.provinceName);

        NSArray * cityArray=tempDic[@"市"];

        for(NSDictionary * cityDic in cityArray)

        {

            NSLog(@"%@",cityDic);

//            _cityArray=cityDic[@"市名"];

//            _codeArray=cityDic[@"編碼"];

        }

    }

}

- (void) changeCity

{

    JRViewController * jrVC=[[JRViewController alloc]init];

    [self.navigationController pushViewController:jrVC animated:YES];

}

@end

JSON檔案

{

    "weatherinfo": {

        "city": "煙台",

        "city_en": "yantai",

        "date_y": "2015年6月15日",

        "date": "",

        "week": "星期一",

        "fchh": "11",

        "cityid": "101120501",

        "temp1": "29℃~19℃",

        "temp2": "31℃~19℃",

        "temp3": "31℃~19℃",

        "temp4": "28℃~18℃",

        "temp5": "26℃~17℃",

        "temp6": "26℃~17℃",

        "tempF1": "84.2℉~66.2℉",

        "tempF2": "87.8℉~66.2℉",

        "tempF3": "87.8℉~66.2℉",

        "tempF4": "82.4℉~64.4℉",

        "tempF5": "78.8℉~62.6℉",

        "tempF6": "78.8℉~62.6℉",

        "weather1": "晴",

        "weather2": "晴轉多雲",

        "weather3": "多雲",

        "weather4": "多雲轉西北雨",

        "weather5": "多雲",

        "weather6": "多雲",

        "img1": "0",

        "img2": "99",

        "img3": "0",

        "img4": "1",

        "img5": "1",

        "img6": "99",

        "img7": "1",

        "img8": "4",

        "img9": "1",

        "img10": "99",

        "img11": "1",

        "img12": "99",

        "img_single": "0",

        "img_title1": "晴",

        "img_title2": "晴",

        "img_title3": "晴",

        "img_title4": "多雲",

        "img_title5": "多雲",

        "img_title6": "多雲",

        "img_title7": "多雲",

        "img_title8": "西北雨",

        "img_title9": "多雲",

        "img_title10": "多雲",

        "img_title11": "多雲",

        "img_title12": "多雲",

        "img_title_single": "晴",

        "wind1": "南風3-4級",

        "wind2": "南風3-4級",

        "wind3": "南風3-4級",

        "wind4": "東南風4-5級",

        "wind5": "東南風4-5級轉3-4級",

        "wind6": "東北風轉東風3-4級",

        "fx1": "南風",

        "fx2": "南風",

        "fl1": "3-4級",

        "fl2": "3-4級",

        "fl3": "3-4級",

        "fl4": "4-5級",

        "fl5": "4-5級轉3-4級",

        "fl6": "3-4級",

        "index": "熱",

        "index_d": "天氣熱,建議着短裙、短褲、短薄外套、T恤等夏季服裝。",

        "index48": "",

        "index48_d": "",

        "index_uv": "強",

        "index48_uv": "",

        "index_xc": "較适宜",

        "index_tr": "适宜",

        "index_co": "較舒适",

        "st1": "28",

        "st2": "18",

        "st3": "30",

        "st4": "17",

        "st5": "30",

        "st6": "17",

        "index_cl": "較适宜",

        "index_ls": "極适宜",

        "index_ag": "不易發"

    }

}

   PS:該案例的關鍵之處在于截取JSON檔案,一層一層解開,直到挑出對自己有用的部分~   (說的簡單,解了一上午,把自己給繞進去了,蠢哭~~~)