读书人

ASIHttpRequest跟json-framework实现j

发布时间: 2012-08-11 20:50:31 作者: rapoo

ASIHttpRequest和json-framework实现json解析(iOS客户端)



这篇日志我会写一个客户端json解析的小例子,下篇日志我会写服务器端的代码。

1、进行必要的准备工作。

下载ASIHttpRequest类库,github上有,https://github.com/pokeb/asi-http-request/

下载json-framework,github上也有,https://github.com/stig/json-framework/

2、将下载的类库添加到Xcode项目中

ASIHttpRequest跟json-framework实现json解析(iOS客户端)

3、添加framework

libz.dylib

CFNetwork.framework

SenTestingKit.framework

SystemConfiguration.framework

MobileCoreServices.framework

4、上面的步骤做好之后,下面就是关键了。

Plain代码
  1. NSURL *url = [NSURL URLWithString:@"http://......(这里是服务端的url)/Default.aspx"];
  2. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  3. [request startSynchronous];
  4. NSString *response = [request responseString];
  5. NSLog(@"%@",response); //这里输出一下,看得到的json字符串是否正确
  6. NSMutableArray *data = [response JSONValue]; //这里得到的json字符串里面含有多个Dictionary
  7. for (NSDictionary *dictionary in data) //对NSMutableArray进行遍历
  8. {
  9. NSLog(@"%@,%@",[dictionary objectForKey:@"number"],[dictionary objectForKey:@"name"]);
  10. }
5、最终在控制台就会输出解析好的键值对应的字符串了。

读书人网 >操作系统

热点推荐