iOS 全國(guó)天氣預(yù)報(bào)json解析

tableviewController.m文件

1.@interface rootTableViewController ()
@property(nonatomic,strong)NSMutableArray *arrM;
@end

@implementation rootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.arrM=[[NSMutableArray alloc]init];

}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillDisappear:YES];
WeatherJson *wjson=[[WeatherJson alloc]init];
self.arrM=[wjson jsonUrlweather];
[self.tableView reloadData];
if (self.arrM==nil) {
    NSLog(@"正在刷新");
}
else
{
    NSLog(@"刷新成功");
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection
(NSInteger)section {
return self.arrM.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *temdic=self.arrM[indexPath.row];
NSString *info=@"MyTableCell";
MyTableCell *cell=[tableView dequeueReusableCellWithIdentifier:info];
if (cell==nil) {
    Class cls=NSClassFromString(info);
    cell=[[cls alloc]initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:info];
}
[cell Mytablecell:temdic];
return cell;
}

自定義tableview

1.MytableCell.h文件
@interface MyTableCell : UITableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
-(void)Mytablecell:(NSDictionary *)dic;
@property(nonatomic,strong)UILabel *label1;
@property(nonatomic,strong)UILabel *label2;
@property(nonatomic,strong)UILabel *label3;
@property(nonatomic,strong)UILabel *label4;
@end

MytableCell.m文件

1.@implementation MyTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    self.label1=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 80, self.frame.size.height)];
    self.label1.textColor=[UIColor colorWithRed:0.089 green:0.240 blue:1.000 alpha:1.000];
    [self addSubview:self.label1];
    
    self.label2=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 280, self.frame.size.height)];
    self.label2.textColor=[UIColor colorWithRed:1.000 green:0.067 blue:0.062 alpha:1.000];
    [self addSubview:self.label2];
    
    self.label3=[[UILabel alloc]initWithFrame:CGRectMake(360, 0, 54, self.frame.size.height)];
    self.label3.textColor=[UIColor colorWithRed:0.332 green:1.000 blue:0.030 alpha:1.000];
    [self addSubview:self.label3];
}
return self;
}
屏幕快照 2016-03-24 下午10.10.52.png
將解析出來(lái)的數(shù)據(jù)通過(guò)key值賦給tableview上
-(void)Mytablecell:(NSDictionary *)dic
{
self.label1.text=dic[@"weatherinfo"][@"city"];
self.label2.text=dic[@"weatherinfo"][@"weather"];
self.label3.text=dic[@"weatherinfo"][@"ptime"];
}
@end

對(duì)網(wǎng)絡(luò)請(qǐng)求下來(lái)的天氣進(jìn)行解析

1.WeatherJson.h文件
@interface WeatherJson : NSObject
@property(nonatomic,strong)NSMutableArray *arrM;
-(NSMutableArray *)jsonUrlweather;
@end
2.WeatherJson.m文件
@implementation WeatherJson
-(NSMutableArray *)jsonUrlweather
{
NSMutableArray *arrM1=[NSMutableArray array];
self.arrM=[NSMutableArray array];
這里是將全國(guó)省會(huì)的天氣預(yù)報(bào)從網(wǎng)上查到相應(yīng)的網(wǎng)站一一列表出來(lái)  
for (int i=1; i<=34; i++) {
    if (i<5) {
        NSString *path=[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/101%02d0100.html",i];
        [self.arrM addObject:path];
    }else
    {
       NSString *path=[NSString stringWithFormat:@"http://www.weather.com.cn/data/cityinfo/101%02d0101.html",i];
        [self.arrM addObject:path];
    }
}        
網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)
for (int i=0; i<34; i++) {
    NSURL *url=[NSURL URLWithString:self.arrM[i]];
    NSData *data=[[NSData alloc]initWithContentsOfURL:url];
    解析
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@  天氣%@ 發(fā)布時(shí)間%@",dic[@"weatherinfo"][@"city"],dic[@"weatherinfo"][@"weather"],dic[@"weatherinfo"][@"ptime"]);
    [arrM1 addObject:dic];
}
return arrM1;
}
@end
屏幕快照 2016-03-27 下午7.34.36.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,881評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評(píng)論 4 61
  • 33歲,一不小心來(lái)到了年少時(shí)對(duì)年齡想象的范圍之外,惶恐起易逝的時(shí)光,惶恐起擁有的東西,然后焦慮、失眠,甚至開(kāi)始考慮...
    槐安閑人閱讀 225評(píng)論 1 1
  • 瘋子要結(jié)婚了。肖揚(yáng)接到瘋子的電話愣了愣,然后連聲恭喜道賀,心想離開(kāi)北京兩年了,這次好兄弟結(jié)婚終于給了他回去的勇氣。...
    菀晴閱讀 304評(píng)論 0 2

友情鏈接更多精彩內(nèi)容