iOS应用开发中数据交互格式是重要知识点。目前比较流行的数据交互格式有JSON (JavaScript Object Notation), XML(Extensible Markup Language)和HTML(Hypertext Markup Language) 。JSON具有占带宽小、易用性、跨平台语言等特点,它在网络数据交互领域占有重要一席。ios5已经支持原生态的JSON库了。
UT确保数据解析方式和结果正确。以下内容介绍了JSON和XML的使用方式。
#import "DataParseTest.h" @interface DataParseTest () <NSXMLParserDelegate>{ int testSize; NSMutableString *currentXmlParserString; } @end @implementation DataParseTest /** * ios5 use native json tool. before ios5 we need to use third library JSONKit. * Json is better than xml data transfer format in mobile devie. */ -(void)testJsonParser{ NSError *error = nil; NSString *dataStr = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:dataStr]; NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSAssert(weatherDic != nil, @"Json value is not nil"); NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; NSEnumerator *emumerator = [weatherInfo keyEnumerator]; id object; while (object = [emumerator nextObject]) { NSLog(@"key is %@",object); id objectValue = [weatherInfo objectForKey:object]; if(objectValue) NSLog(@"value is %@",objectValue); } } /** * test XML parser. */ -(void)testXMLParser{ testSize= 0; currentXmlParserString = [[NSMutableString alloc] init]; NSString *dataStr = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"]; NSData *data = [NSData dataWithContentsOfFile:dataStr]; NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data]; [xmlParser setDelegate:self]; NSAssert([xmlParser parse],@"parse error!"); NSAssert(testSize == 3, @"can not parse totally."); } // Reduce potential parsing errors by using string constants declared in a single place. static NSString * const kCommonElementName = @"COMMON"; static NSString * const kBotanicalElementName = @"BOTANICAL"; static NSString * const kZoneElementName = @"ZONE"; static NSString * const kLightElementName= @"LIGHT"; static NSString * const kPriceElementName= @"PRICE"; static NSString * const kAvaliabilityElementName= @"AVAILABILITY"; #pragma mark - NSXMLParser delegate methods /** you can save you data while do parsing **/ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:kCommonElementName]) { } else if ([elementName isEqualToString:kBotanicalElementName]) { } [currentXmlParserString setString:@""]; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:kCommonElementName]) { NSLog(@"%@",currentXmlParserString); testSize++; } else if ([elementName isEqualToString:kBotanicalElementName]) { } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { [currentXmlParserString appendString:string]; } @end
附上test.xml 和 test.json 文件内容以便UT测试通过。
<?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <PLANT> <COMMON>Bloodroot</COMMON> <BOTANICAL>Sanguinaria canadensis</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$2.44</PRICE> <AVAILABILITY>031599</AVAILABILITY> </PLANT> <PLANT> <COMMON>Columbine</COMMON> <BOTANICAL> <ZONE>======az_piano</ZONE> <LIGHT>======az_huahua</LIGHT> <PRICE>======az_shuxue</PRICE> <AVAILABILITY>======az_sunny_new</AVAILABILITY> </PLANT> <PLANT> <COMMON>======az_sunny</COMMON> <BOTANICAL>======c360_new</BOTANICAL> <ZONE>======c360</ZONE> <LIGHT>sssssshttp://www.letuer.com/assets/img/dzmq.png---http://img10.360buyimg.com/n0/g14/M04/18/12/rBEhVlMk7loIAAAAAAJTku8A1C4AAKSzgIjKykAAlOq487.jpg---com.game.zoonpuzzle---儿童弹电子木琴---让您的宝宝弹电子木琴音乐---11---0===;</LIGHT> <PRICE>======jf_huahua</PRICE> <AVAILABILITY>======xm_huahua</AVAILABILITY> </PLANT> </CATALOG>
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2014年3月4日","date":"","week":"星期二","fchh":"11","cityid":"101010100","temp1":"8℃~-3℃","temp2":"8℃~-3℃","temp3":"7℃~-3℃","temp4":"8℃~-1℃","temp5":"10℃~1℃","temp6":"10℃~2℃","tempF1":"46.4�H~26.6�H","tempF2":"46.4�H~26.6�H","tempF3":"44.6�H~26.6�H","tempF4":"46.4�H~30.2�H","tempF5":"50�H~33.8�H","tempF6":"50�H~35.6�H","weather1":"晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"多云","weather6":"多云","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"1","img10":"99","img11":"1","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"多云","img_title10":"多云","img_title11":"多云","img_title12":"多云","img_title_single":"晴","wind1":"北风4-5级转微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"北风","fx2":"微风","fl1":"4-5级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"寒冷","index_d":"天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。","index48":"冷","index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index_uv":"中等","index48_uv":"中等","index_xc":"较适宜","index_tr":"一般","index_co":"较舒适","st1":"7","st2":"-3","st3":"8","st4":"0","st5":"7","st6":"-1","index_cl":"较不宜","index_ls":"基本适宜","index_ag":"易发"}}