初学ios,现在涉及到json数据的问题,朋友说用SBjson比较方便和快捷,但是网上搜了好多写的都不全面和具体,今天记录下来折腾的这一下午,希望对新手有所帮助。
一:SBJSON
1,由于SBjson属于第三方类库,所以需要我们下载后导入到项目中。
官方下载地址:https://github.com/stig/json-framework/downloads
官方文档(必看) http://stig.github.com/json-framework/api/3.0/index.html
介绍 http://stig.github.com/json-framework/
2,将下载好的文件解压缩,然后在Xcode中File-Add File to "demo"....,将解压后的Classes文件夹添加到项目中来
(注意勾选上copy items..Folders选择第一个)
3,这个时候,项目下会多出一个Classes文件夹,点开后里面全是头文件。不管这些,在你的代码里(如ViewControler.h)#import "SBJSON.h" 注意名字不要错了。这个时候,就可以使用啦。
4,项目中不用覆盖任何方法,相关的类直接拿来就可以使用。JSON类型数据解析成NSMutableDictionary
5,把基本数据类型封装成Json类型。
NSString *username = nameField.text; NSString *password = passwordField.text; NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:username,@"username",password,@"password",nil]; //----------SBJsonWriter---------------------------------- SBJsonWriter *writer = [[SBJsonWriter alloc] init]; NSLog(@"封装mutableDictionary:%@",[writer stringWithObject:userInfo]);输出结果:封装mutableDictionary:{"username":"wda345","password":"132213"}
6,记得多看一下官方的API哈。
SBJSon解析的类型有:
Null -> NSNull
String -> NSMutableString
Array -> NSMutableArray
Object -> NSMutableDictionary
Boolean -> NSNumber
Number -> NSDecimalNumber
如果是其他类型,如,NSDate,那么需要把此转为NSString来处理;NSDictionary,NSArray集合类的基项类型是NSString、NSNumber、NSNull。
传送门:
http://blog.csdn.net/remote_roamer/article/details/7021640
http://kevin1366204484.blog.163.com/blog/static/17196620820121304511302/
http://www.devdiv.com/forum.php?mod=viewthread&tid=122618
http://www.cnblogs.com/xiaodao/archive/2012/02/20/2359469.html
二,网络请求:NSURLConnection
待续....
太麻烦了,有空再研究:
http://hi.baidu.com/jt_one/item/8abdbcfe06d8e65dc9f337eb
三,ASIHTTPRequest 的使用方法
这是一个很火的第三方类库,实用,简单,方便,所以我也放弃了使用官方的 NSURLConnection,改用这个。
此类库下载地址: http://allseeing-i.com/ASIHTTPRequest/ 详细功能方法请登录网站查询文档。
1,首先创建一个single View Applicaiton的项目模版,然后 File----add file to project...弹出的对话框选择解压出来的ASIHTTPRequest文件夹(打开全是.h文件),记得勾选destination选项。
成功添加后,在你的项目下会有一个以ASIHTTPRequest命名的文件夹,打开后全是.h类库
2,此时虽然在项目中导入"ASIHTTPRequest.h" 就能使用它,但是编译的时候会出错误,因为我们的项目还没有手机网络设置,手机服务等类库,这需要我们导入ios自带的类库。
一共需要:SystemConfiguration.framework, MobileCoreServices.framework,和libz.1.2.3.dylib这几个类库。
//检查用户密码,服务器请求 - (IBAction)checkUserIdentity:(id)sender { NSString *username = nameField.text; NSString *password = passwordField.text; //ASIHTTPRequest NSURL *url = [NSURL URLWithString:@"http://192.168.1.4:6401/wap"]; // ASIHTTPRequest *request =[ASIHTTPRequest requestWithURL:url];//Httpget ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];//实例化一个Http Post类 [request setPostValue:username forKey:@"username"]; //发送数据 [request setPostValue:password forKey:@"password"]; //发送数据 [request startSynchronous]; //执行同步请求 NSError *error = [request error]; //如果不错误,输出这些内容 if(!error){ NSString *res = [request responseString]; NSLog(@"res:%@",res); SBJsonParser *parser = [[SBJsonParser alloc] init]; NSError *errorNil = nil; NSMutableDictionary *dict = [parser objectWithString:res error:&errorNil]; NSNumber *status = [dict objectForKey:@"status"]; NSLog(@"status:%@",[dict objectForKey:@"status"]); NSLog(@"message:%@",[dict objectForKey:@"message"]); if([status intValue] == 0){ NSLog(@"此用户没有订购爱贝通业务!"); } else{ NSLog(@"info:%@",[dict objectForKey:@"message"]); NSLog(@"%d",NUMBER); NSURL *targetlist = [NSURL URLWithString:tartgetlistUrl]; ASIHTTPRequest *get = [ASIHTTPRequest requestWithURL:targetlist]; [get startSynchronous]; NSError *tagetlistError= [get error]; if(!tagetlistError){ NSLog(@"tagetList`s res:%@",[get responseString]); [self performSegueWithIdentifier:@"second" sender:self]; // [self.navigationController.navigationController] } } } }5,关于异步请求:
- (IBAction)grabURLInBackground:(id)sender { NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } - (void)requestFinished:(ASIHTTPRequest *)request { // 当以文本形式读取返回内容时用这个方法 NSString *responseString = [request responseString]; // 当以二进制形式读取返回内容时用这个方法 NSData *responseData = [request responseData]; } - (void)requestFailed:(ASIHTTPRequest *)request { NSError *error = [request error]; }