0

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."
John
  • 5,835
  • 8
  • 28
  • 36

2 Answers2

2

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:

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • The -deserializeAsArray: function is deprecated I guess because it's not available anymore in the TouchJSON code. But as JeremyP already stated just use the -deserialize: function and it'll work. – Leon Feb 13 '13 at 08:53
1

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

{'result': [Your array data]}
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Thai
  • 11
  • 2