-1

Need help in Objective C count JSON objects.

I'm using github.com/amarcadet/XMLReader/ to read my XML.

If my JSON returns more than 1 object my code counts the objects perfectly. But if my JSON returns only ONE object it miscounts.

Here is my JSON with 2 objects:

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

};

My objective c code:

NSDictionary *xmlDictionary;

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

NSLog(@"Total questions: %lu", (unsigned long)[questions count]);

The code above return Log: Total questions: 2

If JSON is:

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

};

The code returns Log: Total questions: 3 Which is wrong.

Ronaldo Bahia
  • 576
  • 3
  • 24
  • Where is Envelope, Body, GetInterviewResponse, etc in your json? – thelaws Nov 11 '15 at 21:21
  • Hi, my code returns all those nodes. I did not inserted them in the post for the sake of brevity. – Ronaldo Bahia Nov 11 '15 at 21:23
  • 1
    Do you really believe that _Objective-C_ is _miscounting_??? – matt Nov 11 '15 at 21:29
  • Nope. I just found that XMLReader does not insert parenthesis when only one value is returned. The first JSON ends with parenthesis. The second doesn't. Could be this? https://github.com/amarcadet/XMLReader/issues/14 – Ronaldo Bahia Nov 11 '15 at 21:32
  • We need to see the full json document to eliminate it's contents as the issue. – thelaws Nov 12 '15 at 14:55

1 Answers1

0

Just found that I needed to insert this code for XMLReader to work:

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

And when the array has only one object, everything works fine.

Thanks everyone.

Ronaldo Bahia
  • 576
  • 3
  • 24