JSON解析保存在类中

时间:2023-03-08 20:02:59
JSON解析保存在类中

//my.h
#ifndef __1_Header_h
#define __1_Header_h
#define DEBUG 1
#define aa 1

#ifdef aa
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
#endif

#ifdef bb
#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(...)
#endif
#endif

#endif
//main.m

/*
 {"desc":"OK","status":1000,"data":{"wendu":"25","ganmao":"虽然温度适宜但风力较大,仍较易发生感冒,体质较弱的朋友请注意适当防护。","forecast":[{"fengxiang":"南风","fengli":"4-5级","high":"高温 27℃","type":"晴","low":"低温 20℃","date":"8日星期二"},{"fengxiang":"东南风","fengli":"4-5级","high":"高温 26℃","type":"多云","low":"低温 19℃","date":"9日星期三"},{"fengxiang":"北风","fengli":"4-5级","high":"高温 26℃","type":"晴","low":"低温 18℃","date":"10日星期四"},{"fengxiang":"东北风","fengli":"4-5级","high":"高温 24℃","type":"阴","low":"低温 17℃","date":"11日星期五"},{"fengxiang":"北风","fengli":"4-5级","high":"高温 26℃","type":"多云","low":"低温 19℃","date":"12日星期六"}],"yesterday":{"fl":"4-5级","fx":"西北风","high":"高温 27℃","type":"晴","low":"低温 20℃","date":"7日星期一"},"aqi":"83","city":"大连"}}
     通过城市id获得天气数据,json数据
    http://wthrcdn.etouch.cn/weather_mini?citykey=101070201
 
*/
/*
 //1.创建url地址
 //2.将url数据转换成二进制数据
 //3.将二进制数据转换成对应的字典或者数组
 //4.各种类型转化
 */

#import <Foundation/Foundation.h>
#import "Weather.h"
#import "Data.h"
#import "Foreast.h"
#import "Yesterday.h"
#import "Manager.h"
#import "my.h"

#define PATH @"http://wthrcdn.etouch.cn/weather_mini?citykey=101070201"

int main(int argc, const char * argv[]) {
    @autoreleasepool {

NSURL *url = [[NSURL alloc] initWithString:PATH];
        NSData *data1 = [[NSData alloc] initWithContentsOfURL:url];
        NSDictionary *dictory = [NSJSONSerialization JSONObjectWithData:data1 options:1 error:nil];
        
        Weather *weather = [Weather new];
        Data *data = [Data new];
        Foreast *foreast = [Foreast new];
        Yesterday *yesterday = [Yesterday new];
        weather.date = data;
        data.forecast = foreast;
        data.yesterday = yesterday;
        
        weather.desc = dictory[@"desc"];
        weather.status = dictory[@"status"];
        
        data.wendu = dictory[@"data"][@"wendu"];
        data.ganmao = dictory[@"data"][@"ganmao"];
        data.aqi = dictory[@"data"][@"aqi"];
        data.city = dictory[@"data"][@"city"];
        
        NSArray *array=dictory[@"data"][@"forecast"];
        for (NSDictionary *subDict in array)
        {
            Manager *manager = [Manager new];
            manager.fengxiang = subDict[@"fengxiang"];
            manager.fengli = subDict[@"fengli"];
            manager.high = subDict[@"high"];
            manager.type = subDict[@"type"];
            manager.low = subDict[@"low"];
            manager.date = subDict[@"date"];
            [foreast.marray addObject:manager];
        }
        for (Manager *user in foreast.marray) {
            NSLog(@"fengxiang:%@,fengli:%@,high:%@,type:%@,low:%@,date:%@\n",user.fengxiang,user.fengli,user.high,user.type,user.low,user.date);
        }

NSDictionary *diction1 = dictory[@"data"][@"yesterday"];
        Manager *manager = [Manager new];
        manager.fl1 = diction1[@"fl"];
        manager.fx1 = diction1[@"fx"];
        manager.high1 = diction1[@"high"];
        manager.type1 = diction1[@"type"];
        manager.low1 = diction1[@"low"];
        manager.date1 = diction1[@"date"];
        [yesterday.marray addObject:manager];
        for (Manager *user in yesterday.marray) {
            NSLog(@"fl:%@,fx:%@,high:%@,type:%@,low:%@,date:%@\n",user.fl1,user.fx1,user.high1,user.type1,user.low1,user.date1);
        }
        //NSLog(@"%@",yesterday);
        
        NSLog(@"%@",weather);
    }
    return 0;
}
//Manager.h

//
//  Manager.h
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Manager : NSObject
@property (nonatomic,strong) NSString * fengxiang;
@property (nonatomic,strong) NSString * fengli;
@property (nonatomic,strong) NSString * high;
@property (nonatomic,strong) NSString * type;
@property (nonatomic,strong) NSString * low;
@property (nonatomic,strong) NSString * date;

@property (nonatomic,strong) NSString * fl1;
@property (nonatomic,strong) NSString * fx1;
@property (nonatomic,strong) NSString * high1;
@property (nonatomic,strong) NSString * type1;
@property (nonatomic,strong) NSString * low1;
@property (nonatomic,strong) NSString * date1;
@end
//Manager.m

//
//  Manager.m
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import "Manager.h"

@implementation Manager
-(NSString *)description
{
    return [NSString stringWithFormat:@"fengxiang=%@,fengli=%@,high=%@,type=%@,low=%@,date=%@",self.fengxiang,self.fengli,self.high,self.type,self.low,self.date];
}
@end
//Weather.h

//
//  Weather.h
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Data.h"

@interface Weather : NSObject
@property (nonatomic,strong) NSString * desc;
@property (nonatomic,strong) NSString * status;
@property (nonatomic,strong) Data * date;
@end
//Weather.m

//
//  Weather.m
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import "Weather.h"

@implementation Weather
- (NSString *)description
{
    return [NSString stringWithFormat:@"desc=%@,status=%@,\ndata:%@",self.desc,self.status,self.date];
}
@end
//Yesterday.h

//
//  Yesterday.h
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Yesterday : NSObject
@property (nonatomic,strong) NSMutableArray * marray;
- (instancetype)init;
@end
//Yesterday.m

//
//  Yesterday.m
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import "Yesterday.h"

@implementation Yesterday
- (instancetype)init
{
    if (self = [super init])
    {
        _marray = [[NSMutableArray alloc]init];
    }
    return self;
}

//-(NSString *)description
//{
//    return _marray;
//}
@end
//Foreast.h

//
//  Foreast.h
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Foreast : NSObject
@property (nonatomic,strong) NSMutableArray * marray;
- (instancetype)init;
@end
//Foreast.m

//
//  Foreast.m
//  homework
//
//  Created by hehe on 15/9/8.
//  Copyright (c) 2015年 wang.hehe. All rights reserved.
//

#import "Foreast.h"

@implementation Foreast
- (instancetype)init
{
    if (self = [super init])
    {
        _marray = [[NSMutableArray alloc]init];
    }
    return self;
}

@end
//文件格式

{
    "desc": "OK",
    "status": 1000,
    "data":
    {
        "wendu": "25",
        "ganmao": "虽然温度适宜但风力较大,仍较易发生感冒,体质较弱的朋友请注意适当防护。",
        "forecast": [
                     {
                         "fengxiang": "南风",
                         "fengli": "4-5级",
                         "high": "高温 27℃",
                         "type": "晴",
                         "low": "低温 20℃",
                         "date": "8日星期二"
                     },
                     {
                         "fengxiang": "东南风",
                         "fengli": "4-5级",
                         "high": "高温 26℃",
                         "type": "多云",
                         "low": "低温 19℃",
                         "date": "9日星期三"
                     },
                     {
                         "fengxiang": "北风",
                         "fengli": "4-5级",
                         "high": "高温 26℃",
                         "type": "晴",
                         "low": "低温 18℃",
                         "date": "10日星期四"
                     },
                     {
                         "fengxiang": "东北风",
                         "fengli": "4-5级",
                         "high": "高温 24℃",
                         "type": "阴",
                         "low": "低温 17℃",
                         "date": "11日星期五"
                     },
                     {
                         "fengxiang": "北风",
                         "fengli": "4-5级",
                         "high": "高温 26℃",
                         "type": "多云",
                         "low": "低温 19℃",
                         "date": "12日星期六"
                     }
                     ],
        "yesterday":
        {
            "fl": "4-5级",
            "fx": "西北风",
            "high": "高温 27℃",
            "type": "晴",
            "low": "低温 20℃",
            "date": "7日星期一"
        },
        "aqi": "83",
        "city": "大连"
    }
}