且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XML转换为iOS中的JSON转换

更新时间:2023-02-04 08:43:37

NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:testXMLString error:&parseError];
NSLog(@" %@", xmlDictionary);

此代码未将任何内容转换为JSON。它给你一个NSDictionary。您需要从字典中实际创建JSON数据。试试这个尺寸。

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