UI-json解析网络数据
2015-08-15 08:24
537 查看
// // ViewController.m // 解析数据json // #import "ViewController.h" @interface ViewController () { NSMutableData *_data; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(10, 50, 200, 30); [btn setTitle:@"解析数据" forState:UIControlStateNormal]; btn.backgroundColor = [UIColor cyanColor]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; // Do any additional setup after loading the view, typically from a nib. } -(void)btnClick { //1.将网络地址转化为字符串 NSString * str = @"http://ktx.cms.palmtrends.com/api_v2.php?action=home_list&sa=&uid=10067566&mobile=iphone5&offset=0&count=15&&e=b7849d41b00bbacc9a62544402abed9e&uid=10067566&pid=10053&mobile=iphone5&platform=i"; //2. NSURL *url = [NSURL URLWithString:str]; NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; [connection start]; } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { _data = [[NSMutableData alloc]init]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_data appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *JsonDict = [NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil]; NSArray *listArray = [JsonDict objectForKey:@"list"]; for(NSDictionary *dict in listArray) { NSString *title = [dict objectForKey:@"title"]; NSLog(@"%@",title); } } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"连接失败"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- 一起学CC3200之HttpServer数据传输(中序)
- Linux网络编程(3)——多进程、多线程
- 黑马程序员Java笔记——网络编程
- 利用HTTP-only Cookie缓解XSS
- Tomcat return http status error: 403, Reason Phrase: Forbidden:
- HDU 5383 - Yu-Gi-Oh!(网络流’最小费用最大流)
- HDU 4411 - Arrest(网络流‘最小费用最大流)
- iOS----轻松掌握AFN网络顶级框架
- 网络编程03---JSON和XML
- 应用Wireshark抓取HTTP数据包并分析包内内容
- 打开Windows10网络发现或是文件打印共享
- [译]TCP和UDP的区别
- 网络编程 客户端与服务器数据传输
- 网络编程TCP(二)
- myHttpRequestVolley
- IP网际协议--《TCP/IP详解1》
- 黑马程序员———网络编程问题总结
- https和http的区别
- 安全网络通信SSL
- 网络请求综合应用___搭建网易新闻的头条和详情