Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中XML到JSON的转换_Ios_Xcode_Json_Jquery Mobile_Xml Parsing - Fatal编程技术网

iOS中XML到JSON的转换

iOS中XML到JSON的转换,ios,xcode,json,jquery-mobile,xml-parsing,Ios,Xcode,Json,Jquery Mobile,Xml Parsing,我需要将XML响应转换为JSON,并将sand转换为JSON到javaScript代码 My XML response: <cell> <Result>True</Result> <sguid>02291c402-2220-422b-b199-f92f22e56d2f</sguid> </cell> 我得到的JSON响应如下: { "cell" : { "Result" : {

我需要将XML响应转换为JSON,并将sand转换为JSON到javaScript代码

My XML response:
<cell>
       <Result>True</Result>
       <sguid>02291c402-2220-422b-b199-f92f22e56d2f</sguid>
</cell>
我得到的JSON响应如下:

{
  "cell" : {
    "Result" : {
      "text" : "True"
    },
    "sguid" : {
      "text" : "0391c402-1120-460b-b199-f92fffe56d2f"
    }
  }
}
{
  "cell": {
    "Result": "True",
    "sguid": "02291c402-2220-422b-b199-f92f22e56d2f"
  }
}
我需要像这样的JSON响应:

{
  "cell" : {
    "Result" : {
      "text" : "True"
    },
    "sguid" : {
      "text" : "0391c402-1120-460b-b199-f92fffe56d2f"
    }
  }
}
{
  "cell": {
    "Result": "True",
    "sguid": "02291c402-2220-422b-b199-f92f22e56d2f"
  }
}
因为然后我将这个json发送到javascript代码,我得到一个异常jquery mobile不知道这个解析器,并抛出一个语法错误异常

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


谢谢

我刚刚为您的问题编写了一个函数,我用几个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];

我刚刚为您的问题编写了一个函数,我用几个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];

您在使用XMLReader时遇到的问题。要解决此问题,您可以使用替代XMLReader。

您在使用XMLReader时遇到的问题。要解决此问题,您可以使用它而不是XMLReader。

只需检查该节点是否有名为text的子节点,并且没有子节点即可。然后,您可以提取实际值并跳过XMLReader中的文本节点:您只需检查该节点是否有名为text的子节点,而没有子节点。然后可以提取实际值并跳过XMLReader:中的文本节点,效果非常好。非常感谢。现在我唯一的问题是,如果有一个空项被发送,像这样的clubId:{},我想要的是send clubId:agin,然后我将这个json发送到javascript代码,我得到一个异常jquery mobile不知道这个解析器,并抛出一个语法错误异常。谢谢。然后您应该在函数中添加一个检查,并检查结果是否是nsdictionary,它是否包含0个键。如果是,请用空字符串替换该对象。我想您可以添加该功能,因为上面实现了类似的检查。让我知道如果你有任何问题,我会尝试帮助你的清晰度,你可能会考虑添加功能与另一个功能非常好。非常感谢。现在我唯一的问题是,如果有一个空项被发送,像这样的clubId:{},我想要的是send clubId:agin,然后我将这个json发送到javascript代码,我得到一个异常jquery mobile不知道这个解析器,并抛出一个语法错误异常。谢谢。然后您应该在函数中添加一个检查,并检查结果是否是nsdictionary,它是否包含0个键。如果是,请用空字符串替换该对象。我想您可以添加该功能,因为上面实现了类似的检查。让我知道如果您有任何问题,我会尝试帮助您的清晰度,您可能会考虑添加该功能与另一个功能