【问题标题】:How to convert JSON to XML in objective C?如何在目标 C 中将 JSON 转换为 XML?
【发布时间】:2012-05-23 10:03:47
【问题描述】:

我正在使用 JSON 转换器将 XML 字符串转换为 JSON 字符串。我需要再次将此 JSON 字符串转换回 XML 字符串。有什么具体的方法可以做到吗?

【问题讨论】:

    标签: iphone xml json


    【解决方案1】:

    使用此代码,您可以将 JSON 转换为 NSDictionary

    NSString * jsonString = @"blblblblblb";
    NSStringEncoding  encoding;
    NSData * jsonData = [jsonString dataUsingEncoding:encoding];
    NSError * error=nil;
    NSDictionary * parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    

    请使用以下代码将 NSDictionary 转换为 XML。所以你可以有 JSON 到 XML 的转换器。

    +(NSString*)ConvertDictionarytoXML:(NSDictionary*)dictionary withStartElement:(NSString*)startele
    {
        NSMutableString *xml = [[NSMutableString alloc] initWithString:@""];
        NSArray *arr = [dictionary allKeys];
        [xml appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
        [xml appendString:[NSString stringWithFormat:@"<%@>",startele]];
        for(int i=0;i<[arr count];i++)
        {
            id nodeValue = [dictionary objectForKey:[arr objectAtIndex:i]];
            if([nodeValue isKindOfClass:[NSArray class]] )
            {
                if([nodeValue count]>0){
                    for(int j=0;j<[nodeValue count];j++)
                    {
                        id value = [nodeValue objectAtIndex:j];
                        if([ value isKindOfClass:[NSDictionary class]])
                        {       
                            [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]];
                            [xml appendString:[NSString stringWithFormat:@"%@",[value objectForKey:@"text"]]];
                            [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]];
                        }
    
                    }
                }
            }
            else if([nodeValue isKindOfClass:[NSDictionary class]])
            {
                [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]];
                if([[nodeValue objectForKey:@"Id"] isKindOfClass:[NSString class]])
                    [xml appendString:[NSString stringWithFormat:@"%@",[nodeValue objectForKey:@"Id"]]]; 
                else
                    [xml appendString:[NSString stringWithFormat:@"%@",[[nodeValue objectForKey:@"Id"] objectForKey:@"text"]]]; 
                [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]];
            }
    
            else
            {
                if([nodeValue length]>0){
                    [xml appendString:[NSString stringWithFormat:@"<%@>",[arr objectAtIndex:i]]];
                    [xml appendString:[NSString stringWithFormat:@"%@",[dictionary objectForKey:[arr objectAtIndex:i]]]]; 
                    [xml appendString:[NSString stringWithFormat:@"</%@>",[arr objectAtIndex:i]]];
                }
            }
        }
    
        [xml appendString:[NSString stringWithFormat:@"</%@>",startele]];
        NSString *finalxml=[xml stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    //  NSLog(@"%@",xml);
        return finalxml;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多