开发者

How to parse arrays in array PHP->JSON->XCODE

I'm writing a simple php/mysql script that gets all "ads" I have in my database and returns them in JSON format. This works great if I'm only returning one row, ten the parser will split each string into an index om the array. But now I have this PHP code:

    <?php
   // Database credentials
   $host = 'localhost'; 
   $db = 'project'; 
   $uid = 'root'; 
   $pwd = '';

   $SSN = $_GET['ssn'];

    // Connect to the database server   
    $link = mysql_connect($host, $uid, $pwd) or die("Could not connect");

   //select the json database
   mysql_select_db($db) or die("Could not select database");

   // Create an array to hold our results
   $arr = array();

   //Execute the query
   $rs = mysql_query("SELECT * FROM all_ads WHERE SSN = $SSN ORDER BY datePosted");



   // Add the rows to the array 
   while($obj = mysql_fetch_row($rs)) {
   $arr[] = $obj;
   }

   echo json_encode($arr);

?>

And it returns the following format:

[["4","Hyr ut i natt","321654987","couch_surfing_ads","2011-05-16 13:49:58"],["5","Maybe not","456893214","vacation_resident_ads","2011-05-16 14:22:34"]]

(CONT开发者_运维技巧AINING TWO SEPARATE ADS)

When xcode gets this array back it puts the entire first ad in one index of the array. How do I to also put each array in an array like the JSON output does?

How do I process/deserialize this data is my core question!

Thanks!


I think you just need to have a two layer reference (last line below) unless I'm misunderstanding something:

NSString *response = [[NSString alloc] initWithContentsOfURL:yourSourceUrl];
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSArray *ads = [responseString JSONValue];
NSLog(@"%@",[[ads objectAtIndex:0] objectAtIndex:1]); //Hyr ut i natt
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜