One issue with JSON that has caused problems both here at work and with Celery is the fact that a list of elements is defined by the number of elements. That is, if there are no elements it will be a NIL entry in the Dictionary, one element will return a Dictionary and multiple entries will return an Array. So, programmatically, you don’t know what you are going to get at compile time.
Fortunately, the WebService you are using is will documented… right? Secondly, you are only ever going to get 3 possible results: NIL, NSDictionary or NSArray. As such, you know when something is a list, and can handle these special cases. This is how I am managing it in Celery. If there are better ways, please, let me know.
If the WebService Specification says something might be an Array, I will also treat it as an array. If it’s a single element, I create an array and put that element into it. That way, my business logic only ever has to deal with one type (array).
id object = [dictionary objectForKey:@"key"];
NSArray* myArray = nil;
if([object isKindOfClass:[NSDictionary class]]) {
myArray = [[[NSArray alloc] initWithObjects: object,nil] autorelease];
} else {
myArray = (NSArray*)object;
}