2013-02-20 33 views
1

您好我已經寫了一個使用mysql數據庫的android應用程序,我的問題是php文件以下列格式返回json對象。如何更改json將值返回給android的窗體?

[ 
    [ 
     { 
      "0": "1", 
      "outlet_id": "1", 
      "1": "Big Bazaar", 
      "outlet_name": "Big Bazaar", 
      "2": "12.9285690", 
      "lat": "12.9285690", 
      "3": "77.5831100", 
      "lng": "77.5831100", 
      "4": "images/BigBazaar.png", 
      "outlet_image": "images/BigBazaar.png", 
      "5": "Jayanagar 4th Block", 
      "outlet_location": "Jayanagar 4th Block" 
     } 
    ] 
] 

但我需要它通過它在以下格式。

[ 
    [ 
     { 

      "outlet_id": "1", 
      "outlet_name": "Big Bazaar", 
      "lat": "12.9285690", 
      "lng": "77.5831100", 
      "outlet_image": "images/BigBazaar.png", 
      "outlet_location": "Jayanagar 4th Block" 
     } 
    ] 
] 

這是我的php代碼。

<?php 
error_reporting(0); 

//$url = $_GET['url']; 
//$mR = $_GET['mRequest']; 
//$mOid = $_GET['mOutletId']; 
//$mloc = $_GET['mLocation']; 
//connect to the db 
$user = "root"; 
$pswd = ""; 
$db = "recommendations_db"; 
$host = "localhost"; 
$conn = mysql_connect($host, $user, $pswd); 
mysql_select_db($db); 
//if($mR == 'outlets' && $mloc = 'all'){ 
$query = "SELECT * FROM outlets"; 
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); 

while($row = mysql_fetch_array($result)) 
    { 
    $output[] = array($row); 
    } 
print(json_encode($output)); 
?> 

如何獲取此輸出。請幫忙,因爲我很窮的PHP。

+0

它使客戶端之間沒有什麼區別,JSON仍然將是相同的。 – meh 2013-02-20 11:55:54

回答

4

使用本

while($row = mysql_fetch_assoc($result)) 
{ 
    $output[] = array($row); 
} 

mysql_fetch_assoc — Fetch a result row as an associative array

+1

感謝它的工作就像一個魅力... – user1844638 2013-02-20 12:01:30

+2

+1的不錯的解決方案 – 2013-02-20 12:02:14