2015-10-13 77 views
0

我試圖按照this教程來解析JSONweb service以顯示在應用程序中。PHP/MYSQL/JSON:調整api解析IOS應用程序

,此教程需要輸入貸款從一種名爲KIVA如下:

{"loans":[{"id":961236,"name":"Alhassan","description":{"languages":["en"]},"status":"fundraising","funded_amount":0,"basket_amount":0,"image":{"id":1960690,"template_id":1},"activity":"Personal Purchases","sector":"Personal Use","themes":["Rural Exclusion"],"use":"to buy food and clothing, eliminating pressure to sell maize for low prices at harvest","location":{"country_code":"NG","country":"Nigeria","town":"Kaduna","geo":{"level":"town","pairs":"10 8","type":"point"}},"partner_id":288,"posted_date":"2015-10-13T21:00:03Z","planned_expiration_date":"2015-11-12T21:00:03Z","loan_amount":200,"borrower_count":1,"lender_count":0,"bonus_credit_eligibility":false,"tags":[]} 

從我的服務輸出的電流:

[{"userid":"1","shcom":"hello","lcom":"hello there friend"}] 

我認爲最主要的我的web服務相對於上面的缺失是:

「貸款」:`[在輸出開始時。

所以我想我需要做的是插入在JSON輸出在應用中objective-c代碼的開頭使用「貸款」來創建一個array如下:

NSArray* latestLoans = [json objectForKey:@"loans"]; // gets JSON 

    NSLog(@"loans: %@", latestLoans); // Prints it 

我會怎麼需要做我的PHP代碼來打印出JSON的教程?

感謝您的任何建議:

編輯:

PHP: 

while($row = mysql_fetch_assoc($res)) { 
    $loans[] = $row; 
} 
+0

請更新您的文章並添加生成'json'輸出的'php'代碼。通過你發佈的內容,似乎你正在尋找一個名爲_loans_的關鍵字,這個關鍵字在你的'json'中顯然缺少。因此,在'php'文件中,您必須添加一個名爲_loans_的「key」,併爲您的輸出(現在已包含在您的文章中)分配整個生成的字符串作爲值。 – EhsanT

回答

0

這裏的答案是改變

$loans[] = array('$loan); 
to: 

$loans[] = array('loans'=>$loan); 

這就將 '貸款' 開頭

0

這是一個JSON數組的對象,你需要調整如下所示:

$loans = array(); 
while($row = mysql_fetch_assoc($res)) { 
    $loans['loans'][] = $row; 
} 
echo json_encode($loans); 

注:

  • JSON需求是有效的,您發佈的一個不
  • 使用PDO如果可以的話,mysql_是棄用

例如在PDO:

echo json_encode(array('loans'=>$stmt->fetchAll()));