XML into JSON conversion in iOS
17,737
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSLog(@" %@", xmlDictionary);
This code isn't converting anything to JSON. Its giving you an NSDictionary. You need to actually create the JSON data from the dictionary. Try this on for size.
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",jsonString);
}
Author by
Thangavel
Updated on July 19, 2022Comments
-
Thangavel almost 2 years
I need to convert XML response to JSON.
My XML response:
<commands> <command id="0" name="GetAPPsProducts"> <command_parameters> <command_parameter id="0" name="APPs_Code">ATAiOS</command_parameter> </command_parameters> <command_result> <apps_products> <apps_products id="1"> <apps_code>ATAiOS</apps_code> <apps_product_id>2</apps_product_id> <brand_id>2</brand_id> <brand_desc>Generic</brand_desc> <brand_product_id>2</brand_product_id> <product_id>001-7</product_id> <descrizione>MyTravelApp</descrizione> </apps_products> </apps_products> </command_result> </command>
I am using XMLReader supporting file from this site:
I am using this code to convert XML to JSON
NSError *parseError = nil; NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError]; NSLog(@" %@", xmlDictionary);
I got JSON response like this:
commands = { command = { "command_parameters" = { "command_parameter" = { id = 0; name = "APPs_Code"; text = "\n \n \n \n ATAiOS"; }; text = "\n "; }; "command_result" = { "apps_products" = { "apps_products" = { "apps_code" = { text = "\n \n \n \n ATAiOS"; }; "apps_product_id" = { text = "\n 2"; }; "brand_desc" = { text = "\n Generic"; }; "brand_id" = { text = "\n 2"; }; "brand_product_id" = { text = "\n 2"; }; descrizione = { text = "\n MyTravelApp"; }; id = 1; "product_id" = { text = "\n 001-7"; }; text = "\n "; }; text = "\n "; }; text = "\n "; }; id = 0; name = GetAPPsProducts; text = "\n "; }; text = "\n "; }; text = "\n \n"; };
I need response like this:
{ "commands": { "command": { "-id": "0", "-name": "GetAPPsProducts", "command_parameters": { "command_parameter": { "-id": "0", "-name": "APPs_Code", "#text": "ATAiOS" } }, "command_result": { "apps_products": { "apps_products": { "-id": "1", "apps_code": "ATAiOS", "apps_product_id": "2", "brand_id": "2", "brand_desc": "Generic", "brand_product_id": "2", "product_id": "001-7", "descrizione": "MyTravelApp" }
I get this response while conversion in online. How to get response like this.
Thanks in Advance.
-
Siba Prasad Hota about 11 yearsI tested your Code , Working like a charm , Thanks for answering.
-
Ryan Poolos about 11 yearsGlad I could help. Happy Coding :) Don't forget to accept the answer.
-
marciokoko about 11 years@RyanPoolos Im needing some help...Im getting this:
Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/xml" UserInfo=0x17f9e770 {NSErrorFailingURLKey=http://server.com/sw_icfs.asmx/Response_Sucursales?Cli_Cod=1&Key_String=hW9gvga8ieDBbM5wm4, NSLocalizedDescription=Expected content type {( "text/json", "application/json", "text/javascript" )}, got text/xml}
any ideas? :) -
Ryan Poolos about 11 yearsyou are handing XML to the JSON serializer. You need to use an XML parser instead.
-
marciokoko about 11 years@RyanPoolos thanks, how do I properly format this initial string for the xml parser?
-(void)parseXML{ NSString *xmlString = @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\ <string xmlns=\"http://server.com">\; NSData *xmlData = [xmlString dataUsingEncoding:NSASCIIStringEncoding]; NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData]; [xmlParser setDelegate:self]; [xmlParser parse]; }
-
Ryan Poolos about 11 yearsYour problem is very different from this question. I think it best if you start your own question.