【发布时间】:2014-04-29 21:43:21
【问题描述】:
我正在使用XMLReader 将 XML 转换为 JSON 数据。
我的代码如下。
NSString *XMLDataAsString = [[NSString alloc] initWithBytes: [data mutableBytes] length:[data length] encoding:NSUTF8StringEncoding];
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:XMLDataAsString error:&parseError];
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];
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
bookShelfArray = [[NSMutableArray alloc] init];
bookShelfArray = [[jsonObject valueForKey:@"ArrayOfMagazinesService"] mutableCopy];
NSLog(@"bookShelfArray--%@", bookShelfArray);
}
现在当我有一个对象时,我得到如下输出。
bookShelfArray--{
MagazinesService = {
AppleID = {
text = "";
};
CategoryID = {
text = 6;
};
CategoryName = {
text = "\U0645\U062c\U0644\U0629 \U0643\U0646\U0648\U0632";
};
ID = {
text = 1;
};
IsFree = {
text = true;
};
Name = {
text = "\U0627\U0644\U0639\U062f\U062f \U0627\U0644\U0623\U0648\U0644";
};
Price = {
text = "0.000";
};
Video = {
text = "http://kanyamakan.hardtask.info/Files/Video/1ce00344-d834-41ea-965c-0ef3c3d1214f.mp4";
};
text = "";
};
text = "";
xmlns = "http://tempuri.org/";
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
}
当我有两个或更多对象时,我的输出如下。
bookShelfArray--{
MagazinesService = (
{
AppleID = {
text = "";
};
CategoryID = {
text = 6;
};
CategoryName = {
text = "\U0645\U062c\U0644\U0629 \U0643\U0646\U0648\U0632";
};
ID = {
text = 1;
};
IsFree = {
text = true;
};
Name = {
text = "\U0627\U0644\U0639\U062f\U062f \U0627\U0644\U0623\U0648\U0644";
};
Price = {
text = "0.000";
};
Video = {
text = "http://kanyamakan.hardtask.info/Files/Video/1ce00344-d834-41ea-965c-0ef3c3d1214f.mp4";
};
text = "";
},
{
AppleID = {
text = "com.apple.test";
};
CategoryID = {
text = 6;
};
CategoryName = {
text = "\U0645\U062c\U0644\U0629 \U0643\U0646\U0648\U0632";
};
ID = {
text = 10;
};
IsFree = {
text = false;
};
Name = {
text = dasdasdasd;
};
Price = {
text = "12.000";
};
Video = {
text = "http://kanyamakan.hardtask.info/Files/Video/b3c5c107-3f59-49d1-8f25-5111d1b581ce.mp4";
};
text = "";
}
);
text = "";
xmlns = "http://tempuri.org/";
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
}
如果你看到差异,差异就在
MagazinesService = { & MagazinesService = (
当一个对象时,我有MagazinesService = {
当两个或多个对象时,MagazinesService = (
当两个或更多对象时一切正常。
当它的一个对象应用程序崩溃。崩溃报告如下。
2014-03-22 14:44:25.228 xxxxx[16981:90b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xaf92070
2014-03-22 14:44:25.230 xxxxx[16981:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xaf92070'
【问题讨论】:
标签: objective-c xml-parsing nsmutablearray nsdictionary xmlreader