Ive got a php page reading a mysql db and encoding $json result of the db query. I am using a modified version of the TwitterHelper class used in Presence assignment of CS193P btw. Here is the relevant code:
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "paas") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");
$query = "SELECT * FROM tags";
$arr = array();
$rs = mysql_query("SELECT * FROM tags");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
and the iOS code is this:
userDict = [[NSDictionary alloc] initWithDictionary:[SantiappsHelper fetchInfoForUDID:@"1"]];
which calls:
+ (NSDictionary*)fetchInfoForUDID

NSString *)udid{
NSString *urlString = [NSString stringWithFormat:@"http://www.server.com/app/getusers.php"];
NSLog(@"This is the urlString from (fetchInfoForUDID) SantiappsHelper:%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];
return [self fetchJSONValueForURL:url];
}
which in turn calls this:
+ (id)fetchJSONValueForURL

NSURL *)url{
NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
id jsonValue = [jsonString JSONValue];
NSLog(@"This is the string presented:%@",jsonString);
[jsonString release];
return jsonValue;
NSLog(@"returned jsonvalue, going back to tlvc");
}
The app crashes because im getting the php data in this format:
[
{"id":"1","name":"user","pass":"","udid":"269d40ba 6575b9ec51a7f3237e757c2bcd6bf6","timestamp":"0000-00-00 00:00:00","reglocation":""},
{"id":"2","name":"other","pass":"","udid":"2031aaf b778b3a27635ae38e4315f31bba956805","timestamp":"00 00-00-00 00:00:00","reglocation":""},
{"id":"4","name":"hers","pass":"","udid":"F6869 FB4-2EBE-5D43-A62D-5D4007646764","timestamp":"2010-09-18 11:15:16","reglocation":""},
{"id":"5","name":"his","pass":"","udid":"adf03902c 18974c9d29edf27da779afa66438a3b","timestamp":"2010-09-18 11:18:52","reglocation":""}
]
and trying to put it into a dictionary which json string's format should be:
{
"contributors_enabled" = 0;
"created_at" = "Fri Jun 26 19:45:24 +0000 2009";
description = "";
...
}
so how can i change the format of the json data retrieved?