开发者

How do you parse a JSON array with TouchJSON?

开发者 https://www.devze.com 2023-02-28 01:27 出处:网络
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 jso

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 开发者_JAVA技巧library?

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


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:


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

{'result': [Your array data]}
0

精彩评论

暂无评论...
验证码 换一张
取 消