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": "大连"
    }
}

时间: 2024-10-11 05:54:34

JSON解析保存在类中的相关文章

JSON.net 在实体类中自定义日期的格式

定义日期格式转换类,其继承 IsoDateTimeConverter,代码如下: public class DateTimeConverter : IsoDateTimeConverter { public DateTimeConverter() : base() { // 默认日期时间格式 DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; } public DateTimeConverter(string format) : this() { DateT

iOS中XML、JSON数据结构解析以及第三方类的引用

今天讲到数据结构解析的内容:XML和JSON两种 XML数据结构:是扩展于HTML,主要使用标签对<start></start> 其解析有两种: 1.SAX解析(Simple API for XML).是基于事件驱动的解析方式,逐行解析数据(采用协议回调机制) NSXMLParser是iOS自带的XML解析类.解析过程由NSXMLParserDelegate协议方式回调 解析过程:开始标签-->取值-->结束标签-->取值 使用过程如下: @interface T

IOS中Json解析的四种方法

作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的ios版本,可以使用第三方库来解析Json. 本文将介绍TouchJso

《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

  先看一下我要实现的功能界面:   这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面中有所体现,这里不再赘述,此篇文章主要介绍:我是如何通过js从前台获取数据,然后通过ajax技术向后台一般处理程序传递JSON,后台又是如何获取传来的JSON,并对JSON数据转换,然后将转换后的数据写入数据库的! 介绍这些步骤实现前,先说说自己的辛酸:絮叨辛酸,是因为为了实现这个功能,我真的用了很

【转】IOS中Json解析的四种方法

原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接).此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便. 从IOS5开始,APPLE提供了对json的原生支持(NSJSONSer

【iOS开发-95】JSON反序列化、XML数据解析以及主线程中的UI更新等小细节

我们发送请求后,一般会获得data数据,这个时候我们反序列化即可. 常用的数据格式之一是JSON,格式如: {key:value,key:value,key:value....} (1)因为iOS5之前苹果不支持JSON解析,所以催生很多第三方解析,SBJson以及JSONKit.简单地JSON反序列化示例如下: NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"]; NSURLRequest *request=[NSURLR

通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中  先看一下我要实现的功能界面:   这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面中有所体现,这里不再赘述,此篇文章主要介绍:我是如何通过js从前台获取数

Java中使用Jackson进行JSON解析和序列化

Java中使用Jackson进行JSON解析和序列化 1.添加依赖,在Maven的pom.xml文件中添加以下依赖 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.3</version> </dependency> 2.封装JSON解

Python爬虫项目,获取所有网站上的新闻,并保存到数据库中,解析html网页等

1.需求说明 需求: 爬取虎嗅网站的所有新闻,并保存到数据库中. http://www.huxiu.com 技术: 1.爬虫 获取服务器的资源(urllib) 解析html网页(BeautifulSoup) 2.数据库技术 数据库 MySQLdb 业务逻辑的分析: (1).虎嗅网站的新闻,包括首页和分页信息(下一页) (2).需要从首页的资源和分页的资源中获取每个新闻的url连接 如何获取url: 解析网站html文件,如果A标签的href属性包含 article字段,就表示这是一个新闻 (3)