XML into JSON conversion in iOS

2019-04-15 03:59发布

问题:

I need to convert XML response to JSON and sand to the json To javaScript code.

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

I am using XMLReader supporting file from this site:

XMLReader

I am using this code to convert XML to 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;
}

I got JSON response like this:

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

I need JSON response like this:

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

Because then I send this json to javascript code I get that exception jquery mobile dont know parser this and throws an exception of syntax error.

I've seen programmers use this solution and is helping them but I still get the same result in this solution.

XML into JSON conversion in iOS

thanks

回答1:

I just wrote a function for your problem, I tried it on with a couple of XMLs. Let me know if you find any issues

- (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;
}

And use it like this

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


回答2:

Your problem in using XMLReader. For resolve this problem you can use XMLConverter instead of the XMLReader.