I am trying to create a web service that serves up json from a mysql database through php for display in an iPhone app.
I have a standard mysql/php setup.
The data is in a table with fields and records. It is queried with sql to create a record set. Each record in the recordset is a row.
php
$sql = "SELECT userid,task,longtask FROM tasks WHERE userid = 1 LIMIT 1";
$res = mysql_query($sql) or die(mysql_error());
$tasks = array();
while($row = mysql_fetch_assoc($res)) {
$tasks[] = array('row'=>$row);
}
echo json_encode(array('tasks'=>$tasks));
//
The web service produces the following output:
{"tasks":[{"row":{"userid":"1","task":"send email to Bob","longtask":"include attached memo"}}]}
However, I'm having a lot of trouble getting this to read into IOS suggesting that there might be a better format for the web service.
The structure is different from that in tutorials and other sources I have found for reading the json into IOS (none of which use php/mysql).
Can anyone tell me a better way to structure the json or alternatively code to read this json in iOS to grab the userid, task and other variables.
When I try this, I get an error that it cannot rad row at index:0
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSLog(@"about to print json: %@",json);
NSMutableArray *getElement = [json objectForKey:@"tasks"];
for (NSDictionary *dict in getElement) {
NSArray *array = [dict objectForKey:@"row"];
NSString *str = [array objectAtIndex:0];
}
Thanks in advance for any suggestions.