JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。具体介绍:http://www.json.org/json-zh.html
Json简单说就是javascript中的对象和数组,所以这两种结构就是对象和数组2种结构,通过这两种结构可以表示各种复杂的结构
1、对象:对象在js中表示为“{}”扩起来的内容,数据结构为 {key:value,key:value,…}的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理 解,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。
2、数组:数组在js中是中括号“[]”扩起来的内容,数据结构为 ["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种
四种Json解析的方法实现如下:
首先建立一个新的SingleViewApplication应用,实现如下的布局:
建立输出口和动作,得到如下的代码:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *txtView;
- (IBAction)btnTouchJson:(id)sender;
- (IBAction)btnSBJson:(id)sender;
- (IBAction)btnIOSJson:(id)sender;
- (IBAction)btnJsonKit:(id)sender;
@end
方法一、 TouchJSON一直是最慢的。
在github上下载touchJson,在工程中新建一个组url,里面在建一个touchjson的组,将下载下来的source文件夹中的文件放到touchjson中;
在viewController中引入头文件
#import "CJSONDeserializer.h"
并在动作
- (IBAction)btnTouchJson:(id)sender中加入代码
//使用TouchJson来解析北京的天气
//获取API接口
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
//定义一个NSError对象,用于捕获错误信息
NSError *error;
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSLog(@"jsonString--->%@",jsonString);
//将解析得到的内容存放字典中,编码格式为UTF8,防止取值的时候发生乱码
NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
//因为返回的Json文件有两层,去第二层内容放到字典中去
NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
NSLog(@"weatherInfo--->%@",weatherInfo);
//取值打印
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
运行程序,点击touchJson按钮,textView中就会显示天气情况;并且也会输出Log,如下图:
方法二:直接用苹果官方提供的JSON库,如果你的app只支持iOS 5.0以上系统,那么直接用苹果官方提供的JSON库:NSJSONSerialization 库即可。
在动作 - (IBAction)btnIOSJson:(id)sender中加入
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
运行程序,点击IOSJson,便会在textView中显示天气情况,并且输出Log;
方法三、看来SBJSON一直屈居倒数第二,大众们准备放弃吧。。3.1版本及以上的SBJson,它将支持ARC,在文件夹url中新建文件夹SBJason,将下载下来的 .m和.h文件拷贝进来,引用头文件SBJson.h,
在动作
- (IBAction)btnSBJson:(id)sender中加入如下代码:
//使用SBJson解析南阳的天气
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
NSError *error = nil;
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];
NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
方法四:如果你的app要支持iOS 5.0以下的系统,那么我个人推荐JSONKit,不过JSONKit本身做了很多内存上的优化,所以不支持ARC,你在使用时可以对其加上 -fno-objc-arc 的编译标志即可,下面将会介绍。
在url文件夹中新建文件夹JSONKit,将2个.m和.h文件拷贝进来,引用头文件,并且在下图中对此文件的ARC进行设置(双击即可设置):
- (IBAction)btnJsonKit:(id)sender中假如如下代码
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *err = nil;
NSURLResponse *response = nil;
NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
//此步骤只是得到了第一层数据
NSDictionary *resultsDictionary = [jsonData objectFromJSONData];
//因为返回的Json文件有两层,去第二层内容放到字典中去
NSDictionary *weatherInfo = [resultsDictionary objectForKey:@"weatherinfo"];
self.txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"resultsDictionary%@", resultsDictionary);
时间测试,简单可用Log打印时间测试大概的时间(精确到s),也可参见下面链接的测试方法。
demo环境:xcode6.0,demo下载网址 http://download.csdn.net/detail/avel__/8425739
本文参考博文链接:http://blog.csdn.net/xiaoxiangzhu660810/article/details/8222302