我正在實施基於客戶端的應用程序。在那我有一個XML字符串。我需要將其轉換爲JSON格式併發送到服務器。我不知道如何轉換。你們可以給我建議任何文件或想法嗎?如何使用iPhone sdk傳遞XML字符串到JSON
在此先感謝, 錢德拉。
我正在實施基於客戶端的應用程序。在那我有一個XML字符串。我需要將其轉換爲JSON格式併發送到服務器。我不知道如何轉換。你們可以給我建議任何文件或想法嗎?如何使用iPhone sdk傳遞XML字符串到JSON
在此先感謝, 錢德拉。
第1步:讀取XML到的NSDictionary:http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
第2步:轉換的NSDictionary成JSON:http://code.google.com/p/json-framework/
正如史蒂夫說,兩個步驟是那些,我離開你的一些代碼,也許可以幫助你多一點:
// 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);
此外,我在XMLReader文件中進行了一些修改,使其更符合我的方式。 – 2016-06-05 14:33:13