How do you parse a JSON array with TouchJSON?

1.4k views Asked by At

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."
2

There are 2 answers

1
JeremyP On BEST ANSWER

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:

0
Thai On

I think most parsers frown with array data. You'll have to return a hash:

{'result': [Your array data]}