title: php -> JSON -> iPhone
body: <p>I have an iPhone app which sends a request to a url posting a variable called submit:</p>
  
  <pre><code>+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url{
      NSMutableArray *questions = [[NSMutableArray alloc] init];
      //connect to database given by url
      //NSError        *error = nil;
      //NSURLResponse  *response = nil;
      NSMutableString* myRequestString = [[NSMutableString string]initWithFormat:@"section=%@", section];
      NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: url]]; 
      [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
      [request setHTTPMethod: @"POST"];
      //post section 
      [request setHTTPBody: myRequestData];
  
      //store them in the array;
      return [questions autorelease];
  }
  </code></pre>
  
  <p>My php file:</p>
  
  <pre><code>&lt;?php
  
  //connect to database
  function connect() {
    $dbh = mysql_connect ("localhost", "abc1", "12345") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db("PDS", $dbh); 
    return $dbh;
  }
  
  //store posted data
  if(isset($_POST['section'])){
    $dbh = connect();
    $section = $_POST['section'];
    $query = mysql_query("SELECT * FROM QUESTIONS WHERE sectionId = $section;") or die("Error: " . mysql_error());;
  
    $rows = array();
    while($r = mysql_fetch_assoc($query)) {
      $rows[] = $r;
    }
    echo '{"questions":'.json_encode($rows).'}';
    mysql_close();
  }
  ?&gt;
  </code></pre>
  
  <p>I have built a model class (Question) in objective c which has the exact properties that each row element has in the rows associative array. </p>
  
  <p>My questions are:</p>
  
  <p>1) How can I read the echo'd JSON array elements and their relative attributes in objective C? </p>
  
  <p>2) How can I create an array of Question objects and map each one to an element in the rows array? </p>
  
  <p>3) What do I have to write in my method "+(NSMutableArray*)getQuestions:(NSString*)section from: (NSString*) url" to capture the reply from the php (the echo)?</p>
  
  <p>EDIT:</p>
  
  <p>Here is the output of the php:</p>
  
  <p><a href="http://dev.speechlink.co.uk/David/get_questionstest.php" rel="nofollow">http://dev.speechlink.co.uk/David/get_questionstest.php</a></p>
  
  <p>UPDATE</p>
  
  <p>Changed method to use ASIHTTPRequest - Cannot deserialise JSON string:</p>
  
  <pre><code>//method to 
  +(NSDictionary*)getQuestions:(NSString*)sectionId from: (NSString*) url{
      NSDictionary *questions;
      NSURL *link = [NSURL URLWithString:url];
      ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
      [request setPostValue:sectionId forKey:@"section"];
      NSError *error = [request error];
      [request startAsynchronous];
  
      if (!error) {       
          //NSString *response = [request responseString];
          //store them in the dictionary
          NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
          NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
          questions = [json objectFromJSONString];
          NSLog(@"Data: %@", questions); //outputs Data: (null)
          [json release];
          [request release];      
      }else{
          //UIAlertView to warn users there was an error
      }               
  
      return questions;   
  }
  </code></pre>