[英]XML to JSON conversion in ios
我正在嘗試將Xcode中的簡單xml文檔轉換為JSON。 問題是不斷返回nil。
這是我的代碼:
NSURL *url = [[NSURL alloc] initFileURLWithPath:@"http://www.w3schools.com/xml/note.xml"];
NSData *xmlData = [[NSData alloc] initWithContentsOfURL:url];
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLData:xmlData error:&parseError];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:xmlDictionary
options:NSJSONWritingPrettyPrinted
error:&error];
NSLog(@"%@", jsonData);
錯誤信息:
[NSJSONSerialization dataWithJSONObject:options:error:]: value parameter is nil'
正如rmaddy所說,請嘗試打印parseError。 如果parseError為Nil,則嘗試打印xmlDictionary,並檢查字典是否正確創建。
如果xmlDictionary也是Nil,則檢查xmlData,這應該沒問題,在這種情況下,必須檢查dictionaryForXMLData方法是否檢查任何條件。
希望我能幫上忙。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.