【问题标题】:Objective C - XMLReader miscounting ArrayObjective C - XMLReader 错误计数数组
【发布时间】:2015-11-11 20:53:26
【问题描述】:

在 Objective C 计数 JSON 对象方面需要帮助。

我正在使用 github.com/amarcadet/XMLReader/ 来读取我的 XML。

如果我的 JSON 返回超过 1 个对象,我的代码会完美地计算对象。 但是,如果我的 JSON 只返回一个对象,它就会记错。

这是我的带有 2 个对象的 JSON:

QuestionTotal = {
Question = ({
    Description = {
        text = "bla bla";
    };
    id = {
        text = "123";
    };
    ord = {
        text = 1;
    };
}, {
    Description = {
        text = "blu blu";
    };
    id = {
        text = "456";
    };
    ord = {
        text = 2;
    };
});

};

我的目标 c 代码:

NSDictionary *xmlDictionary;

NSMutableArray *questions = [[[[[[[self.xmlDictionary objectForKey:@"Envelope"] objectForKey:@"Body"] objectForKey:@"GetInterviewResponse"] objectForKey:@"GetInterviewResult"] objectForKey:@"Obj"] objectForKey:@"QuestionsTotal"] objectForKey:@"Question"];

NSLog(@"问题总数:%lu", (unsigned long)[问题数]);

上面的代码返回 Log: Total questions: 2

如果 JSON 是:

QuestionTotal = {
Question = {
    Description = {
        text = "bla bla";
    };
    id = {
        text = "123";
    };
    ord = {
        text = 1;
    };
};

};

代码返回 Log: Total questions: 3 这是错误的。

【问题讨论】:

  • 您的 json 中的 Envelope、Body、GetInterviewResponse 等在哪里?
  • 嗨,我的代码返回所有这些节点。为了简洁起见,我没有将它们插入帖子中。
  • 你真的相信Objective-Cmiscounting???
  • 不。我刚刚发现只有一个值返回时,XMLReader 不会插入括号。第一个 JSON 以括号结尾。第二个没有。可能是这个? github.com/amarcadet/XMLReader/issues/14
  • 我们需要查看完整的 json 文档以消除它的内容作为问题。

标签: objective-c json nsarray xmlreader


【解决方案1】:

刚刚发现我需要插入此代码才能使 XMLReader 工作:

if (![list isKindOfClass:[NSArray class]])
{
    // if 'list' isn't an array, we create a new array containing our object
    list = [NSArray arrayWithObject:list];
}

当数组只有一个对象时,一切正常。

谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    相关资源
    最近更新 更多