title: Parse JSON collection from Rails in Objective-C on iPhone
body: <p>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 <code>Round</code> instance for each JSON object, and stick those <code>Round</code> objects into an NSArray so I can load this into a <code>UITableView</code>. So if there's a more straight-forward way to do that than what I'm about to show (which currently is NOT working, btw) please let me know.</p>
  
  <p>The Rails API is returning a collection that looks something like this:</p>
  
  <pre><code>[
    { 
      "round": { "course_title": "Title A", "result": "+8" }
    },
    { 
      "round": { "course_title": "Title B", "result": "+4" }
    },
    ...
  ]
  </code></pre>
  
  <p>I'm also using ASIHTTPRequest and I can successfully get the response using:</p>
  
  <pre><code>NSString *responseString = [request responseString];
  </code></pre>
  
  <p>But from there, I cannot seem to get anywhere. Here's more-or-less what TouchJSON suggests:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I thought from there I would be able to loop through the dictionary and create the object mappings using my <code>Round</code> class. But maybe that's the wrong approach altogether.</p>
  
  <p>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:</p>
  
  <p>1) Should TouchJSON be able to accept an array of JSON objects like what my API is returning?</p>
  
  <p>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.</p>
  
  <p>3) Am I going about this whole process correctly?</p>
  
  <p>Any tips/advice would be much appreciated.</p>