JSON parsing is saved in the class

Source: Internet
Author: User
Tags date1

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": +, "data": {"Wendu": "", "" Ganmao ":" Although the temperature is suitable but the wind is larger, still more prone to colds, weak physique of friends please pay attention to appropriate protection. " "," forecast ": [{" Fengxiang ":" South Wind "," Fengli ":" 4-5 Level "," High ":" 27 ℃ "," type ":" Clear "," Low ":" Lo "," "" "Date": "8th Tuesday" ℃ " Fengxiang ":" Southeast Wind "," Fengli ":" Level 4-5 "," High ":" Hot 26 ℃ "," type ":" Cloudy "," Low ":" Cryogenic 19 ℃ "," date ":" 9th Wed "},{" Fengxiang ":" North Wind "," Fengli ":" Level 4-5 "," High ":" 26 ℃ "," type ":" Sunny "," low ":" Cool 18 ℃ "," Date ":" 10th Thursday "},{" Fengxiang ":" Northeast "," Fengli ":" 4-5 "," High ":" 24 ℃ "," type ":" Yin "," Low ":" 17 ℃ "," date ":" 11th Friday "},{" Fengxiang ":" North Wind "," Fengli ":" 4-5 Levels "," High ":" Hot 26 ℃ "," type ":" Cloudy "," Low ":" Cryogenic 19 ℃ "," Date ":" 12th Saturday "}]," Yesterday ": {" FL ":" Level 4-5 "," FX ":" Northwest Wind "," High ":" Hot 27 ℃ "," type ":" Clear "," low ":" Cryogenic 20℃ "," Date ":" 7th Monday "}," AQI ":" The "," City ":" Dalian "}
Get weather data by City ID, JSON data
http://wthrcdn.etouch.cn/weather_mini?citykey=101070201

*/
/*
1. Create a URL address
2. Converting URL data to binary data
3. Convert binary data to a corresponding dictionary or array
4. Various types of conversions
*/

#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) Wang.hehe 2015. 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) Wang.hehe 2015. 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
File format

{?
"desc": "OK",
"Status": 1000,
"Data":
{?
"Wendu": "25",
"Ganmao": Although the temperature is suitable but the wind is larger, still more prone to colds, weak physique friends please pay attention to appropriate protection. ",
"Forecast": [?
{?
"Fengxiang": "South Wind",
"Fengli": "Level 4-5",
"High": "27 ℃",
"Type": "Fine",
"Low": "Lo 20 ℃",
"Date": "8th Tuesday"
},
{?
"Fengxiang": "Southeast Wind",
"Fengli": "Level 4-5",
"High": "26 ℃",
"Type": "Cloudy",
"Low": "Lo 19 ℃",
"Date": "Wednesday, 9th"
},
{?
"Fengxiang": "The North Wind",
"Fengli": "Level 4-5",
"High": "26 ℃",
"Type": "Fine",
"Low": "Lo 18 ℃",
"Date": "Thursday, 10th"
},
{?
"Fengxiang": "Northeast Wind",
"Fengli": "Level 4-5",
"High": "24 ℃",
"Type": "Yin",
"Low": "Lo 17 ℃",
"Date": "Friday, 11th"
},
{?
"Fengxiang": "The North Wind",
"Fengli": "Level 4-5",
"High": "26 ℃",
"Type": "Cloudy",
"Low": "Lo 19 ℃",
"Date": "12th Saturday"
}
],
"Yesterday":
{?
"FL": "Level 4-5",
"FX": "Northwest Wind",
"High": "27 ℃",
"Type": "Fine",
"Low": "Lo 20 ℃",
"Date": "Monday, 7th"
},
"AQI": "83",
"City": "Dalian"
}
}

JSON parsing is saved in the class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.