2013-04-04 76 views
1

我需要将XML响应转换为JSON并转换为json转换为javaScript代码。将XML转换为iOS中的JSON转换

My XML response: 
<cell> 
     <Result>True</Result> 
     <sguid>02291c402-2220-422b-b199-f92f22e56d2f</sguid> 
</cell> 

我使用的XMLReader从这个网站支持文件:

的XMLReader

我使用这个代码,以XML转换成JSON:

+ (NSString*) XMLToJson:(CXMLDocument *)xmlDocument 
{ 
    NSError *error = nil; 

    NSArray *resultNodes = [xmlDocument nodesForXPath:@"//cell" error:&error]; 

    if(error) 
     NSLog(@"%@",error.description); 

    CXMLNode *cellNode = [resultNodes objectAtIndex:0]; 

    NSLog(@"%@",cellNode.XMLString); 

    NSError *parseError = nil; 
    NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:cellNode.XMLString error:&parseError]; 

    NSLog(@"%@", xmlDictionary); 

    //{print this. 
    // cell =  { 
    //  Result =   { 
    //   text = True; 
    //  }; 
    //  sguid =   { 
    //   text = "0391c402-1120-460b-b199-f92fffe56d2f"; 
    //  }; 
    // }; 
    //} 




    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary 
                 options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string 
                 error:&error]; 
    if(error) 
     NSLog(@"%@",error.description); 

    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", jsonString); 

    return jsonString; 
} 

我得到了这样的JSON响应:

{ 
    "cell" : { 
    "Result" : { 
     "text" : "True" 
    }, 
    "sguid" : { 
     "text" : "0391c402-1120-460b-b199-f92fffe56d2f" 
    } 
    } 
} 

我需要这样的JSON响应:

{ 
    "cell": { 
    "Result": "True", 
    "sguid": "02291c402-2220-422b-b199-f92f22e56d2f" 
    } 
} 

因为那时候我送这个JSON JavaScript代码我得到异常jQuery Mobile的不知道解析器这并抛出语法错误的异常。

我见过程序员使用这个解决方案并且帮助他们,但我仍然在这个解决方案中得到相同的结果。

XML into JSON conversion in iOS

感谢

+0

您只需检查该节点是否具有名为“text”的子节点并且没有子节点。然后你可以提取真正的值,并跳过XMLReader中的“文本”节点:) – Bartu 2013-04-04 12:53:29

+0

如何检查? – 2013-04-04 13:34:43

回答

3

我只写你的问题的功能,我尝试了与一对夫妇个XML的。我们如果您发现任何问题

- (NSMutableDictionary *)extractXML:(NSMutableDictionary *)XMLDictionary 
{ 
    for (NSString *key in [XMLDictionary allKeys]) { 
     // get the current object for this key 
     id object = [XMLDictionary objectForKey:key]; 

     if ([object isKindOfClass:[NSDictionary class]]) { 
      if ([[object allKeys] count] == 1 && 
       [[[object allKeys] objectAtIndex:0] isEqualToString:@"text"] && 
       ![[object objectForKey:@"text"] isKindOfClass:[NSDictionary class]]) { 
       // this means the object has the key "text" and has no node 
       // or array (for multiple values) attached to it. 
       [XMLDictionary setObject:[object objectForKey:@"text"] forKey:key]; 
      } 
      else { 
       // go deeper 
       [self extractXML:object]; 
      } 
     } 
     else if ([object isKindOfClass:[NSArray class]]) { 
      // this is an array of dictionaries, iterate 
      for (id inArrayObject in (NSArray *)object) { 
       if ([inArrayObject isKindOfClass:[NSDictionary class]]) { 
        // if this is a dictionary, go deeper 
        [self extractXML:inArrayObject]; 
       } 
      } 
     } 
    } 

    return XMLDictionary; 
} 

而且使用这样的

NSDictionary *clearXML = [[self extractXML:[yourParsedXMLDictionary mutableCopy]] copy]; 
+0

工作得很好。谢谢。 现在我只能说,如果有一个空的项目像这样 “clubId”发来的问题:{} 我想是送 “clubId”:“” 因为阿恩然后我这个JSON发送到JavaScript代码我得到这个异常jQuery的移动不知道解析器,并引发语法错误的异常。 谢谢。 – 2013-04-08 05:02:49

+0

然后您应该在函数中添加一个检查,并检查结果是否是nsdictionary并且它包含0个键。如果是这样,请用空的nsstring替换该对象。我猜你可以添加该功能,因为上面实现了类似的检查。让我知道如果你遇到任何问题,我会尽力帮助你(为了清楚起见,你可以考虑用另一个功能来增加这个功能) – Bartu 2013-04-08 11:37:44

3

你使用XMLReader的问题我知道。要解决此问题,您可以使用XMLConverter而不是XMLReader。

+0

非常棒的解决方案,谢谢。 :) – 2013-12-11 08:21:57

+0

这帮助了一个lottt并保存了一段时间的解析... – 2015-09-29 12:11:48