Deserializing a complex JSON result (array of dictionaries) with TouchJSON
- by jpm
I did a few tests with TouchJSON last night and it worked pretty well in general for simple cases. I'm using the following code to read some JSON content from a file, and deserialize it:
NSString *jsonString = [[NSString alloc] initWithContentsOfFile:@"data.json"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
NSDictionary *items = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSLog(@"total items: %d", [items count]);
NSLog(@"error: %@", [error localizedDescription]);
That works fine if I have a very simple JSON object in the file (i.e. a dictionary):
{"id": "54354", "name": "boohoo"}
This way I was able to get access to the array of values, as I wanted to get the item based on its index within the list:
NSArray *items_list = [items allValues];
NSString *name = [items_list objectAtIndex:1];
(I understand that I could have fetched the name with the dictionary API)
Now I would like to deserialize a semi-complex JSON string, which represents an array of dictionaries. An example of such a JSON string is below:
[{"id": "123456", "name": "touchjson"}, {"id": "3456", "name": "bleh"}]
When I try to run the same code above against this new content in the data.json file, I don't get any results back. My NSLog() call says "total items: 0", and no error is coming back in the NSError object.
Any clues on what is going on? I'm completely lost on what to do, as there isn't much documentation available for TouchJSON, and much less usage examples.