ASIHttpRequest+SBJson完成Http请求,以及json数据解析
发布时间:2020-12-16 19:27:20 所属栏目:百科 来源:网络整理
导读:一、添加ASIHttpRequest 方法:http://www.jb51.cc/article/p-yvppbqaw-tm.html 二、添加SBJson SBJson的开放源码在github上托管。 三、以上工作完成之后,下面就是敲代码了 (1)首先加入头文件 #import "ASIHTTPRequest.h"#import "SBJson.h" (2)Http请
一、添加ASIHttpRequest 方法:http://www.52php.cn/article/p-yvppbqaw-tm.html
二、添加SBJson SBJson的开放源码在github上托管。
三、以上工作完成之后,下面就是敲代码了 (1)首先加入头文件
#import "ASIHTTPRequest.h" #import "SBJson.h" (2)Http请求
NSURL *url = [NSURL URLWithString:@"http://......."]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; //对应的同步请求[request startSynchronous]; (3)请求完成后,会调用requestFinished方法
-(void) requestFinished: (ASIHTTPRequest *) request { NSString *responseData = [request responseString]; //获得服务器response的数据 //解析Json数据.... } (4)Json数据处理 (a)处理一条记录 测试用的字符串是:responseData =@"{"user":{"id":21,"name":"xxx"}}";
SBJsonParser * parser = [[SBJsonParser alloc] init]; NSError * error = nil; NSMutableDictionary *jsonDic = [parser objectWithString:responseString error:&error]; NSLog(@"%@",[jsonDic objectForKey:@"user" ]); NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"user"]; NSLog(@"%@",[dicUserInfo objectForKey:@"id"]); NSLog(@"%@",[dicUserInfo objectForKey:@"name"]); (b)处理多条记录 测试用的字符串是:
SBJsonParser * parser = [[SBJsonParser alloc] init]; NSError * error = nil; NSArray *memberList = [parser objectWithString:responseString error:&error]; for(NSDictionary *member in memberList) { NSLog(@"dibitsID = %d",[[member objectForKey:@"dibitsID"] integerValue]); NSLog(@"type = %d",[[member objectForKey:@"type"] integerValue]); NSLog(@"time = %@",[member objectForKey:@"updated"]); NSLog(@"n"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |