如何使用iPhone sdk将XML字符串转换为JSON

时间:2022-04-24 21:49:56

I am implementing a client based application. In that I have an xml string. I need to convert it to JSON format and send to the server. I have no idea on converting this. Can you guys please suggest me any documentation or idea to this?

我正在实现基于客户端的应用程序。我有一个xml字符串。我需要将其转换为JSON格式并发送到服务器。我不知道转换它。你能告诉我任何文件或想法吗?

2 个解决方案

#1


11  

Step #1: Read XML into NSDictionary: http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

步骤#1:将XML读入NSDictionary:http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

Step #2: Convert NSDictionary into JSON: http://code.google.com/p/json-framework/

第2步:将NSDictionary转换为JSON:http://code.google.com/p/json-framework/

#2


0  

As Steve said the two steps are those, I leave you a bit of code, maybe can help you a bit more:

正如史蒂夫所说的那两个步骤,我给你留下了一些代码,也许可以帮助你一点:

// Don't forget the imports ;)
#import "XMLReader.h"

// You must have a XML string from somewhere
NSString XMLString = yourXML;
// I remove all returns and tabs from the text, after i would be annoying if you don't remove it
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\t" withString:@""];

// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLString error:&parseError];

NSError *error;
self.dataParsed = [NSJSONSerialization dataWithJSONObject:xmlDictionary
                                                   options: NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

// Print the dictionary
NSLog(@"%@", xmlDictionary);

#1


11  

Step #1: Read XML into NSDictionary: http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

步骤#1:将XML读入NSDictionary:http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

Step #2: Convert NSDictionary into JSON: http://code.google.com/p/json-framework/

第2步:将NSDictionary转换为JSON:http://code.google.com/p/json-framework/

#2


0  

As Steve said the two steps are those, I leave you a bit of code, maybe can help you a bit more:

正如史蒂夫所说的那两个步骤,我给你留下了一些代码,也许可以帮助你一点:

// Don't forget the imports ;)
#import "XMLReader.h"

// You must have a XML string from somewhere
NSString XMLString = yourXML;
// I remove all returns and tabs from the text, after i would be annoying if you don't remove it
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
XMLString = [XMLString stringByReplacingOccurrencesOfString:@"\t" withString:@""];

// Parse the XML into a dictionary
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLString error:&parseError];

NSError *error;
self.dataParsed = [NSJSONSerialization dataWithJSONObject:xmlDictionary
                                                   options: NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

// Print the dictionary
NSLog(@"%@", xmlDictionary);