现在的位置: 首页 > 综合 > 正文

iPhone开发笔记(9)ASIHttpRequest和json-framework实现json解析(iOS客户端)

2013年09月06日 ⁄ 综合 ⁄ 共 861字 ⁄ 字号 评论关闭

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

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

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

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

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

    

3、添加framework

      libz.dylib

     CFNetwork.framework

     SenTestingKit.framework

     SystemConfiguration.framework

     MobileCoreServices.framework

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

     

    NSURL *url = [NSURL URLWithString:@"http://......(这里是服务端的url)/Default.aspx"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSString *response = [request responseString];
    NSLog(@"%@",response);  //这里输出一下,看得到的json字符串是否正确
    NSMutableArray *data = [response JSONValue]; //这里得到的json字符串里面含有多个Dictionary
    for (NSDictionary *dictionary in data) //对NSMutableArray进行遍历
    {        
        NSLog(@"%@,%@",[dictionary objectForKey:@"number"],[dictionary objectForKey:@"name"]);
    }

5、最终在控制台就会输出解析好的键值对应的字符串了。


抱歉!评论已关闭.