博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML数据转JSON数据
阅读量:6039 次
发布时间:2019-06-20

本文共 5703 字,大约阅读时间需要 19 分钟。

1:JSON转XML---通常把JSON数据写到Plist文件即可;

2:XML数据转JSON--

  使用开源类 XMLReader 先XML数据先转换为 NSDictionary 即可

3: 开源类XMLReader 

1 // 2 //  XMLReader.h 3 // 4 // 5 /* 6     本类使用方法: 7         1:在使用的页面里面 导入本类 #import "XMLReader.h" 8         2: 调用XMLReader 的两个类方法 就可以了,就可以返回字典数据; 9         3:得到 NSDictionary数据,就是JSON 数据了;10  11  */12 #import 
13 14 15 @interface XMLReader : NSObject
16 {17 NSMutableArray *dictionaryStack;18 NSMutableString *textInProgress;19 NSError **errorPointer;20 }21 22 //XML Data数据转换为 字典的方法;23 + (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer;24 + (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer;25 26 @end
View Code
1 //  2 //  XMLReader.m  3 //  4   5 #import "XMLReader.h"  6   7 NSString *const kXMLReaderTextNodeKey = @"text";  8   9 @interface XMLReader (Internal) 10  11 - (id)initWithError:(NSError **)error; 12 - (NSDictionary *)objectWithData:(NSData *)data; 13  14 @end 15  16  17 @implementation XMLReader 18  19 #pragma mark - 20 #pragma mark -公有的方法 21  22 //调用以下两个类方法即可; 23  24 + (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error 25 { 26     XMLReader *reader = [[XMLReader alloc] initWithError:error]; 27     NSDictionary *rootDictionary = [reader objectWithData:data]; 28     [reader release]; 29     return rootDictionary; 30 } 31  32 + (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error 33 { 34     NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; 35     return [XMLReader dictionaryForXMLData:data error:error]; 36 } 37  38 #pragma mark - 39 #pragma mark 以下为 私有处理方法,可以不关注; 40  41 - (id)initWithError:(NSError **)error 42 { 43     if (self = [super init]) 44     { 45         errorPointer = error; 46     } 47     return self; 48 } 49  50 - (void)dealloc 51 { 52     [dictionaryStack release]; 53     [textInProgress release]; 54     [super dealloc]; 55 } 56  57 - (NSDictionary *)objectWithData:(NSData *)data 58 { 59     // Clear out any old data 60     [dictionaryStack release]; 61     [textInProgress release]; 62      63     dictionaryStack = [[NSMutableArray alloc] init]; 64     textInProgress = [[NSMutableString alloc] init]; 65      66     // Initialize the stack with a fresh dictionary 67     [dictionaryStack addObject:[NSMutableDictionary dictionary]]; 68      69     // Parse the XML 70     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 71     parser.delegate = self; 72     BOOL success = [parser parse]; 73      74     // Return the stack's root dictionary on success 75     if (success) 76     { 77         NSDictionary *resultDict = [dictionaryStack objectAtIndex:0]; 78         return resultDict; 79     } 80      81     return nil; 82 } 83  84 #pragma mark - 85 #pragma mark NSXMLParserDelegate methods 86  87 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 88 { 89     // Get the dictionary for the current level in the stack 90     NSMutableDictionary *parentDict = [dictionaryStack lastObject]; 91  92     // Create the child dictionary for the new element, and initilaize it with the attributes 93     NSMutableDictionary *childDict = [NSMutableDictionary dictionary]; 94     [childDict addEntriesFromDictionary:attributeDict]; 95      96     // If there's already an item for this key, it means we need to create an array 97     id existingValue = [parentDict objectForKey:elementName]; 98     if (existingValue) 99     {100         NSMutableArray *array = nil;101         if ([existingValue isKindOfClass:[NSMutableArray class]])102         {103             // The array exists, so use it104             array = (NSMutableArray *) existingValue;105         }106         else107         {108             // Create an array if it doesn't exist109             array = [NSMutableArray array];110             [array addObject:existingValue];111 112             // Replace the child dictionary with an array of children dictionaries113             [parentDict setObject:array forKey:elementName];114         }115         116         // Add the new child dictionary to the array117         [array addObject:childDict];118     }119     else120     {121         // No existing value, so update the dictionary122         [parentDict setObject:childDict forKey:elementName];123     }124     125     // Update the stack126     [dictionaryStack addObject:childDict];127 }128 129 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName130 {131     // Update the parent dict with text info132     NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];133     134     // Set the text property135     if ([textInProgress length] > 0)136     {137         [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];138 139         // Reset the text140         [textInProgress release];141         textInProgress = [[NSMutableString alloc] init];142     }143     144     // Pop the current dict145     [dictionaryStack removeLastObject];146 }147 148 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string149 {150     // Build the text value151     [textInProgress appendString:string];152 }153 154 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError155 {156     // Set the error pointer to the parser's error object157     *errorPointer = parseError;158 }159 160 @end
View Code

4:GitHub下载:

 

转载于:https://www.cnblogs.com/cocoajin/archive/2013/05/16/3082507.html

你可能感兴趣的文章
SpringMVC、MyBatis声明式事务管理
查看>>
开发者详解:端游及手游服务端的常用架构
查看>>
JavaScript History对象
查看>>
在 Windows 下安装 Oracle 11g XE (Express Edition)
查看>>
ListView优化
查看>>
【原创】 PostgreSQL 实现MySQL 的auto_increment 字段
查看>>
vs2015添加vc助手
查看>>
检测点1.1
查看>>
android--------阿里 AndFix 热修复
查看>>
control.add()
查看>>
Sublime text3中配置Github
查看>>
Asp.net,C# 加密解密字符串
查看>>
网页视频播放器插件源码
查看>>
2019-4-23 plan
查看>>
[编解码] 关于base64编码的原理及实现
查看>>
WinDbg配置和使用基础
查看>>
转:Object-Runtime的基本数据类型
查看>>
JMJS系统总结系列----Jquery分页扩展库(五)
查看>>
Excel技巧之——英文大小写转换(转)
查看>>
Google 翻译的妙用
查看>>