开发者

Parse JSON collection from Rails in Objective-C on iPhone

I'm using TouchJSON to parse the output of a JSON Rails API, but am having difficulties. The overall goal is to loop through the response, parse the JSON, create a Round instance for each JSON object, and stick those Round objects into an NSArray so I can load this into a UITableView. So if there's a more straight-forward way to开发者_如何学C do that than what I'm about to show (which currently is NOT working, btw) please let me know.

The Rails API is returning a collection that looks something like this:

[
  { 
    "round": { "course_title": "Title A", "result": "+8" }
  },
  { 
    "round": { "course_title": "Title B", "result": "+4" }
  },
  ...
]

I'm also using ASIHTTPRequest and I can successfully get the response using:

NSString *responseString = [request responseString];

But from there, I cannot seem to get anywhere. Here's more-or-less what TouchJSON suggests:

NSString *jsonString = [request responseString]; // [{"round":{...}}, ..., {"round:{...}}]
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:nil];

// then I do this...
NSLog(@"JSON: %@", dictionary); // JSON: null

I thought from there I would be able to loop through the dictionary and create the object mappings using my Round class. But maybe that's the wrong approach altogether.

My thoughts are that the JSON being returned from Rails is an array of JSON objects, so maybe that's why the JSON parser doesn't recognize it as valid JSON? From this, I have two questions:

1) Should TouchJSON be able to accept an array of JSON objects like what my API is returning?

2) Is it possible to cast the responseString to an NSArray so I can loop through each "round" and parse the JSON that way? If I remove the first and last characters from the response string (i.e. "[" and "]") the JSON parser will only grab the first "round" in the collection.

3) Am I going about this whole process correctly?

Any tips/advice would be much appreciated.


TouchJSON presents three main ways to go from JSON to an Obj-C object. They are all present in the header for CJSONDeserializer which you're already using:

- (id)deserialize:(NSData *)inData error:(NSError **)outError;

- (id)deserializeAsDictionary:(NSData *)inData error:(NSError **)outError;
- (id)deserializeAsArray:(NSData *)inData error:(NSError **)outError;

The first one will return return whatever, either a dictionary, array, string or whatever the root type of the JSON is.

The other two expect a dictionary or an array and will complain (i.e. return nil and give you an NSError) if they don't get the right data.


The deserializeAsDictionary:error: method of CJSONDeserializer relies on the scanJSONDictionary:error: method of CJSONScanner. This method expects the "dictionary" to be an object literal. Therefore, your data must start with a {. Since your data is an array, you would want to use the deserializeAsArray:error: method of CJSONDeserializer.


Read the documentation carefully, your code is incorrect. It should look like this:

NSData *jsonData = [request responseData]
NSArray *rounds = [[CJSONDeserializer deserializer] deserialize:jsonData error:nil];

// then I do this...
NSLog(@"JSON: %@", rounds);

You could also have used:

NSArray *rounds = [[CJSONDeserializer deserializer] deserializeAsArray:jsonData error:nil];

However your absolute BIGGEST mistake was passing nil for error. You could have avoided going to stackoverflow at ALL if you had passed something in for NSError and then checked that.


With the right tools, this is WAY simpler than you're making it. I do this sort of thing all the time.

Use Stig's JSON framework, and import the NSString category that provides the JSONValue method.

Then inside your ASIHTTPRequest response handler code, go thusly:

NSMutableArray *roundlist = [NSMutableArray array];
NSArray *results = [[request responseString] JSONValue];
for (NSDictionary *item in results) {
    Round *myRound = [item objectForKey:@"round"];
    //don't actually do the above. Do whatever you do to instantiate a 'Round'.
    [roundlist addObject:myRound];
}
[self.tableView reloadData];

EDIT: Geezo. Objection noted re valueForKey: vs objectForKey:. I updated my code sample, and I think we all learned something here.

I also didn't mean any offense with the phrase "with the right tools". OP was looking to simplify his code, and the RIGHT TOOL for that is the library with the simplest interface. I have nothing against TouchJSON per se, but JSON Framework has the simpler interface.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜