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.
I'm the author of TouchJSON.
Your outermost object should be a dictionary and NOT an array. Anything other than a dictionary is not legal. If you have to have an array as the outermost object then use the method (which is technically deprecated, but isn't going any where soon)
- (id)deserialize:(NSData *)inData error:(NSError **)outError;
See: http://www.json.com/json-schema-proposal/ for more information abotu what is and is not legal JSON.
This isn't an answer, but a pointer to a different framework:
http://code.google.com/p/json-framework/
I've been using it quite a bit lately, serializing and de-serializing complex data structures from third-party services such as Google Local and between my own Objective-C and Perl code with absolutely no problems. Not to mention that the API is ridiculously easy to deal with.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With