嗨,我编写了一个与 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