1

嗨,我编写了一个使用 mysql db 的 android 应用程序,我的问题是 php 文件以以下格式返回 json 对象。

[
    [
        {
            "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 方面很差。

4

1 回答 1

4

用这个

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

mysql_fetch_assoc — Fetch a result row as an associative array

于 2013-02-20T11:55:15.713 回答