Search code examples
jsonios4touchjson

How do you parse a JSON array with TouchJSON?


I'm using TouchJSON. Made a call to my Rails app to fetch all "posts" at localhost:3000/posts.json. This returns a JSON array, surrounded by square brackets. I'm currently set up to convert the jsonString into an NSDictionary, but that fails due to the square brackets.

NSString *jsonString=[self jsonFromURLString:@"http://localhost:3000/posts.json"];
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    self.dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

What's the best way to turn this result into an NSArray using the TouchJSON library?

Error Domain=kJSONScannerErrorDomain Code=-101 "Could not scan dictionary. Dictionary that does not start with '{' character."

Solution

  • Why don't you just use the -deserialize: method and then figure out what kind of object you have after its done?

    Or to answer your specific question, use -deserializeAsArray: