标签:android des style http os io strong 数据
一。XML解析
1.获取网络数据,并放置到XMLParser解析器中。
NSString *urlStr=@"http://api.zbw.vc/api/MobileDesktop/GetUpdateAdvertisinToMobileIndex?ct=xml"; NSURL *url=[NSURL URLWithString:urlStr]; NSXMLParser *parser=[[NSXMLParser alloc] initWithContentsOfURL:url];
2.设置解析代理。
[parser setDelegate:self];
3.开始解析。
[parser parse];
其一般必须走以下方法:(类似Android的xmlParser解析)
#prag mark -开始解析标签、多次调用 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict #prag mark -解析元素、多次调用 -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string #prag mark -解析元素标签、多次调用 -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict #prag mark -解析出错 -(void)parser:(NSXMLParser *)parser validationErrorOccurred:(NSError *)validationError #prag mark -开始解析文档、1次 -(void)parserDidStartDocument:(NSXMLParser *)parser #prag mark -解析文档结束、1次 -(void)parserDidEndDocument:(NSXMLParser *)parser
二。JSON解析
JSON解析可通过NSJSONSerialization类的类方法来解析,如下
//获取网络数据 NSURL *url=[NSURL URLWithString:@"http://api.zbw.vc/api/MobileDesktop/GetUpdateAdvertisinToMobileIndex?ct=json"]; NSData *data=[NSData dataWithContentsOfURL:url]; //通过Json解析为array NSArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //进行数据存储 NSArray *pathDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES); NSString *path=[pathDir[0] stringByAppendingPathComponent:@"ad.plist"]; // NSLog(@"%@",array[0]); [array writeToFile:path atomically:YES]; //取出数据并打印 NSMutableArray *arr2=[NSMutableArray array]; for (NSDictionary *dic in array) { CSZWallpaper *wallPaper=[[CSZWallpaper alloc] init]; wallPaper.imageUrl=[dic objectForKey:@"imageurl"]; wallPaper.isUpdate=[((NSNumber *)[dic objectForKey:@"isupdate"]) integerValue]; wallPaper.type=[((NSNumber *)[dic objectForKey:@"type"]) integerValue]; [arr2 addObject:wallPaper]; } NSLog(@"解析后的对象为:>>%@",arr2);
标签:android des style http os io strong 数据
原文地址:http://blog.csdn.net/qq285016127/article/details/38409173