0

嗨,我编写了一个与 php 交互以从数据库中获取数据的 android 应用程序。

我需要 json 以这种格式返回数据。

[
    {
        "outlet_id"    :"1",
        "outlet_desc"  :"BigBazaar",
        "outlet_loc"   :"Jayanagar4thBlock",
        "outlet_image" :"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FBigBazaar.png",
        "recommended_products": [
                                    {
                                        "item_id":"3",
                                        "item_desc":"Dettol",
                                        "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
                                    }
                                ]
    },
    {
        "outlet_id":"2",
        "outlet_desc":"FoodWorld",
        "outlet_loc":"Basavanagudi",
        "outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FFoodWorld.png","
        recommended_products":[
                                {
                                    "item_id":"3",
                                    "item_desc":"Dettol",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"
                                },
                                {
                                    "item_id":"3",
                                    "item_desc":"Colgate",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"
                                }
                              ]
    },
    {
        "outlet_id":"5",
        "outlet_desc":"Total",
        "outlet_loc":"MurgeshPalaya",
        "outlet_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FTotal.png",
        "recommended_products":[
                                {
                                    "item_id":"3",
                                    "item_desc":"Dettol",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FDettol.png"


                                },
                                {
                                    "item_id":"3",
                                    "item_desc":"Colgate",
                                    "item_image":"http%3A%2F%2Flocalhost%2FFlutura%2FPHP%2FAssets%2Fimages%2FColgate.png"

                                }
                               ]
     }
]

但现在它以以下格式给出。

[
    [
        {
            "outlet_id": "1",
            "outlet_name": "Big Bazaar",
            "lat": "12.9285690",
            "lng": "77.5831100",
            "outlet_image": "images/BigBazaar.png",
            "outlet_location": "Jayanagar 4th Block"
        }
    ],
    [
        {
            "outlet_id": "2",
            "outlet_name": "Food World",
            "lat": "12.9392350",
            "lng": "77.5780680",
            "outlet_image": "images/FoodWorld.png",
            "outlet_location": "Basavanagudi"
        }
    ],
    [
        {
            "outlet_id": "5",
            "outlet_name": "Total",
            "lat": "12.9589340",
            "lng": "77.6571610",
            "outlet_image": "images/Total.png",
            "outlet_location": "Murugeshpalya"
        }
    ],
    [
        {
            "outlet_id": "6",
            "outlet_name": "Big Bazaar",
            "lat": "12.9324963",
            "lng": "77.6228463",
            "outlet_image": "images/BigBazaar.png",
            "outlet_location": "Koramangala"
        }
    ],
    [
        {
            "outlet_id": "7",
            "outlet_name": "Food World",
            "lat": "12.9785055",
            "lng": "77.6379353",
            "outlet_image": "images/FoodWorld.png",
            "outlet_location": "Indira Nagar"
        }
    ]
]

这是我的 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_assoc($result))
{
  $output[] = array($row); 
}
print( json_encode($output) );
?>

我怎样才能修改上面的 php 代码以第一种格式返回。请帮助,因为我在 php 方面不太好。

表结构。

奥特莱斯表。

它包含以下字段。

outlet_id outlet_name lat lng outlet_image outlet_location

建议表。

它包含以下字段。

item_id outlet_id

物品表。

它包含以下字段。

item_id item_name item_image item_price category_id

4

2 回答 2

1

好的,这是缺少的子数组。您想从多维关联数组创建 JSON 字符串,而不是对象。

有更快的方法来选择这些行,但这很容易理解。

$query = "SELECT * FROM outlets";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());

$output=array(); # don't leave it undefined
while($row = mysql_fetch_assoc($result))
{
    # guessing this query
    $query2 = "SELECT * FROM recommended_products WHERE outlet_id=".$result['outlet_id'];

    # now we create the sub array recommended_products
    $row['recommended_products']=array();
    $result2 = mysql_query($query2) or die("Unable to verify user because : " . mysql_error());
    while($row2 = mysql_fetch_assoc($result2)){
        $row['recommended_products'][]=$row2;
    }

    # then add the modified row to the output
    $output[] = $row; 
}
print( json_encode($output) );

此外, mysql_ 已折旧。可能没有真正需要急于求成,但至少值得考虑转换为 mysqli 或 PDO。

于 2013-02-21T02:47:21.817 回答
0

更改$output[] = array($row);$output[] = $row;

于 2013-02-21T02:30:47.570 回答