IOS开发之----NSDictionary,JSON和XML互相转换
2015-08-13 14:57
465 查看
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self test];
// Override point for customization after application launch.
return YES;
}
-(void)test {
//XML文本范例
NSString *testXMLString = @"Cake0.55RegularChocolateBlueberryNoneGlazedSugar";
NSLog(@"xml string[\n%@\n]", testXMLString);
// 解析XML为NSDictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
// 打印 NSDictionary
NSLog(@"%@", xmlDictionary);
//NSDictionary转换为Data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&parseError];
//Data转换为JSON
NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonData string[\n%@\n]", str);
//字符组转换为NSDictionary
NSDictionary *jsonDict = [str objectFromJSONString];
//NSDictionary转换为XML的plist格式
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:jsonDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:NULL];
//Data转换为NSString输出 编码为UTF-8
NSLog(@"XML: %@", [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]);
NSLog(@"%@",[XMLWriter XMLStringFromDictionary:jsonDict withHeader:NO]);
}
//其中用到了三个类库,分别为
1,JSONKit https://github.com/johnezang/JSONKit
2,XMLWriter https://github.com/ahmyi/XMLWriter
3,XMLReader https://github.com/amarcadet/XMLReader
源码下载地址:http://download.csdn.net/download/p709723778/5725585
下面连接是XML转换为Dictionary
https://github.com/nicklockwood/XMLDictionary http://download.csdn.net/detail/p709723778/6706331
{
[self test];
// Override point for customization after application launch.
return YES;
}
-(void)test {
//XML文本范例
NSString *testXMLString = @"Cake0.55RegularChocolateBlueberryNoneGlazedSugar";
NSLog(@"xml string[\n%@\n]", testXMLString);
// 解析XML为NSDictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
// 打印 NSDictionary
NSLog(@"%@", xmlDictionary);
//NSDictionary转换为Data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary options:NSJSONWritingPrettyPrinted error:&parseError];
//Data转换为JSON
NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonData string[\n%@\n]", str);
//字符组转换为NSDictionary
NSDictionary *jsonDict = [str objectFromJSONString];
//NSDictionary转换为XML的plist格式
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:jsonDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:NULL];
//Data转换为NSString输出 编码为UTF-8
NSLog(@"XML: %@", [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]);
NSLog(@"%@",[XMLWriter XMLStringFromDictionary:jsonDict withHeader:NO]);
}
//其中用到了三个类库,分别为
1,JSONKit https://github.com/johnezang/JSONKit
2,XMLWriter https://github.com/ahmyi/XMLWriter
3,XMLReader https://github.com/amarcadet/XMLReader
源码下载地址:http://download.csdn.net/download/p709723778/5725585
下面连接是XML转换为Dictionary
https://github.com/nicklockwood/XMLDictionary http://download.csdn.net/detail/p709723778/6706331
相关文章推荐
- iOS8扩展插件Extension的开发
- ios接入facebook及集成plugin-x
- iOS中关于登录验证方面的问题的解决
- [iOS]字符串转字典
- 【转】iOS调试技巧
- iOS第三方支付-支付宝支付
- 4000 获取键盘的高度变化
- IOS 将毫秒数转换为年月日时分秒的时间
- 关于iOS中的懒加载
- iOS 集合的深复制与浅复制
- iOS利用Runtime自定义控制器POP手势动画(经典)
- iOS 之GCD串行和并发队列的理解
- iOS学习过程中遇到的一些有用的小功能(8/13更新)
- iostream.h和iostream 区别
- 一个phonegap门外汉的phonegap初体验
- 一个phonegap门外汉的phonegap初体验
- ios x264编译问题 No working C compiler found
- IOS下Category添加属性字段
- ios 打开图片库和相机选择图片界面修改为简体中文
- 千锋扣丁学堂 如何学习iOS