您的位置:首页 > Web前端 > JavaScript

Xcode4.4.1安装SBjson以及使用的相关方法

2012-08-27 18:53 531 查看
初学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。

传送门:
/article/7745715.html

http://kevin1366204484.blog.163.com/blog/static/17196620820121304511302/

http://www.devdiv.com/forum.php?mod=viewthread&tid=122618

/article/5078511.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这几个类库。

1. 选中项目
2. 选中目标
3. 跳转到“Build Phases”标签
4. 展开“Link Binary With Libraries”分组
5. 点击“+”添加类库如下图所示:






3,好了,大功告成,开始在你的.h文件下#import导入吧!

4,同步请求(httpPost)
这是ASIHTTPRequest最简单的一种使用模式,发送startSynchronous消息后即开始在同一线程中执行HTTP请求,线程将一直等待直到请求结束(请求成功或者失败)。通过检查error属性可以判断请求是否成功或者有错误发生。

要获取返回的文本信息,调用responseString方法。如果下载的是二进制文件,例如图片、MP3,则调用responseData方法,可以得到一个NSData对象。

这是我的一个点击button,校验密码的例子,大家注意:NSURL中的http:// 不要忘记了,否则即便没有错误,不会有任何提示。(ASIFormDataRequest类需要导入)
//检查用户密码,服务器请求
- (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,关于异步请求:
一般情况下,应该优先使用异步请求代替同步请求,当在主线程中使用ASIHTTPRequest同步请求,应用程序的界面会锁定,无法进行任何操作,直到请求完成。

- (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];
}


传送门:
官方文档:http://allseeing-i.com/ASIHTTPRequest/How-to-use
http://www.dreamingwish.com/dream-2011/powerful-asihttprequest-library-c.html

http://www.cocoachina.com/bbs/read.php?tid-9455-keyword-ASIHttpRequest.html

http://hi.baidu.com/xiezuan/item/dc595338f21e9a89f4e4ad42

http://www.cocoachina.com/iphonedev/sdk/2010/1011/2159.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: