2011-05-16 85 views
1

我在寫一個簡單的php/mysql腳本,獲取我的數據庫中的所有「廣告」,並以JSON格式返回它們。這很好,如果我只返回一行,10解析器會將每個字符串拆分爲數組中的索引。但現在我有這個PHP代碼:如何解析數組中的數組PHP-> JSON-> XCODE

<?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); 

?> 

,並返回以下格式:

[["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"]] 

(包含兩個獨立廣告)

當Xcode中得到這個陣列後面,它把整個第一廣告位於數組的一個索引中。 我該如何將每個數組放入類似JSON輸出的數組中呢?

如何處理/反序列化這些數據是我的核心問題!

謝謝!

回答

3

我認爲你需要有一個兩層參考值(見下最後一行)除非我誤解的東西:

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

我愛你=)謝謝!摸索出偉大的! – 2011-05-16 14:23:29