【问题标题】:XMLReader not working when have only 1 data只有 1 个数据时 XMLReader 不起作用
【发布时间】: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


    【解决方案1】:

    首先,我对你提到的图书馆一无所知。但是,当它有一个对象时,它看起来像是在生成一个字典,而当它看到多个对象时,它会生成一个字典数组。这并不罕见。

    查看该库的文档,看看它是否有生成单项数组的选项。

    或者,您可以在代码中自行处理以解析 JSON 数据。

    从崩溃报告中,您显然假设MagazineService 对象是一个数组(或至少响应objectAtIndex:)。

    解决方案是在您期望 JSON 对象为 MagazinesService 但始终返回数组时接受。

    - (NSArray*)magazinesServiceFromJSONObject:(id)jsonObject
    {
        id object = [jsonObject objectForKey:@"MagazinesService"];
        if ([object isKindOfClass:[NSArray class]]) return object;
        return @[object];
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 2013-01-30
      • 2020-02-13
      • 2021-04-23
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多