• XML与JSON的转换


    -(void)test {
        
        //XML文本范例
        NSString *testXMLString = @"Cake0.55RegularChocolateBlueberryNoneGlazedSugar";
        
        NSLog(@"xml string[ %@ ]", 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[ %@ ]", 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
     
     
     
    下面连接是XML转换为Dictionary
    https://github.com/nicklockwood/XMLDictionary
    http://download.csdn.net/detail/p709723778/6706331
  • 相关阅读:
    Java多线程编程初识— —2
    Java多线程编程初识
    Java XML解析技术
    Java读取Properties文件的六种方法
    Java对象的序列化与反序列化
    文言写作
    阅读理解主题综合训练
    记叙文阅读的六大题型详解(全部转换成法事情人"法事情人")
    读书类、美景类作文
    开头技法
  • 原文地址:https://www.cnblogs.com/dududuzhaoji/p/5472305.html
Copyright © 2020-2023  润新知