1.现在的解析数据类型越来越多的是json数据,现在让我们来解析下json数据:
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式。
有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验(点击打开链接)。此网站不仅可以检测Json代码中的错误,而且可以以视图形式显示json中的数据内容,很是方便。
从IOS5开始,APPLE提供了对json的原生支持(NSJSONSerialization),但是为了兼容以前的ios版本,可以使用第三方库来解析Json。
本文将介绍TouchJson、 SBJson 、JSONKit 和 iOS5所支持的原生的json方法,解析国家气象局API,TouchJson和SBJson需要下载他们的库
TouchJson包下载: http://download.csdn.net/detail/enuola/4523169
SBJson 包下载: http://download.csdn.net/detail/enuola/4523177
JsonKit包的下载: https://github.com/johnezang/JSONKit
下载完成之后我们将我们需要的东西导入到工程中:
TouchJson这个包里面都包含了touchJSon和SBJson , JsonKit要下载:
touchjson遇到的问题有:很多文件都不是支持arc,你要把对应的文件加入:-fno-objc-arc 就可以了
jsonkit:有的文件也不支持arc,同样的处理方式加入-fno-objc-arc
Jsonkit还有两个问题:Jsonkit不支持isa
//array->isa = _JKArrayClass;
object_setClass(array, _JKArrayClass);
// dictionary->isa = _JKDictionaryClass;
object_setClass(dictionary, _JKDictionaryClass)
2.现在开始介绍四种解析Json的数据格式:
在故事版上拉取界面:如图
在viewController.m的实现文件如下:
//
// ViewController.m
// json解析
#import "ViewController.h"
#import "CJSONDeserializer.h"
#import "SBJson.h"
#import "JSONKit.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *Textview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)touchJSON:(id)sender {
//获取url地址
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
//定义NSError ,扑捉异常
NSError *error;
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
//用字典保存数据(解析数据)utf-8的格式
NSDictionary *Rootdic = [[CJSONDeserializer deserializer]deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];
//NSDictionary 字典里面包括两层
// {
// "weatherinfo": {
// "city": "北京",
// "cityid": "101010100",
// "temp1": "15℃",
// "temp2": "5℃",
// "weather": "多云",
// "img1": "d1.gif",
// "img2": "n1.gif",
// "ptime": "08:00"
// }
// }
NSDictionary *weatherInfo = [Rootdic objectForKey:@"weatherinfo"];
NSLog(@"weatheInfo:%@",weatherInfo);
self.Textview.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"cityid"],[weatherInfo objectForKey:@"temp1"], [weatherInfo objectForKey:@"temp2"], [weatherInfo objectForKey:@"ptime"]];
}
- (IBAction)SBJSON:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.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.Textview.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"cityid"],[weatherInfo objectForKey:@"temp1"], [weatherInfo objectForKey:@"temp2"], [weatherInfo objectForKey:@"ptime"]];
}
- (IBAction)JOSN:(id)sender {
NSError *error;
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/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.Textview.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是:%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo字典里面的内容为--》%@", weatherDic );
}
- (IBAction)JOSNKIT:(id)sender {
//如果json是“单层”的,即value都是字符串、数字,可以使用objectFromJSONString
NSString *json1 = @"{\"a\":123, \"b\":\"abc\"}";
NSLog(@"json1:%@",json1);
NSDictionary *data1 = [json1 objectFromJSONString];
NSLog(@"json1.a:%@",[data1 objectForKey:@"a"]);
NSLog(@"json1.b:%@",[data1 objectForKey:@"b"]);
//[json1 release];
//如果json有嵌套,即value里有array、object,如果再使用objectFromJSONString,程序可能会报错(测试结果表明:使用由网络或得到的php/json_encode生成的json时会报错,但使用NSString定义的json字符串时,解析成功),最好使用objectFromJSONStringWithParseOptions:
NSString *json2 = @"{\"a\":123, \"b\":\"abc\", \"c\":[456, \"hello\"], \"d\":{\"name\":\"张三\", \"age\":\"32\"}}";
NSLog(@"json2:%@", json2);
NSDictionary *data2 = [json2 objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSLog(@"json2.c:%@", [data2 objectForKey:@"c"]);
NSLog(@"json2.d:%@", [data2 objectForKey:@"d"]);
// [json2 release];
}
@end
希望对你有帮助!!