duanjiao8871 2013-02-20 11:50
浏览 23
已采纳

如何更改json将值返回到android的形式?

Hi i have written an android app which uses mysql db my problem is that the php file returns the json object in the following format.

[
    [
        {
            "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"
        }
    ]
]

But i need it to pass it in the following format.

[
    [
        {

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

This is my php code.

<?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) );
?>

How to get this output. Please help as i am poor in php.

  • 写回答

1条回答 默认 最新

  • douyinghuo8874 2013-02-20 11:55
    关注

    use this

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

    mysql_fetch_assoc — Fetch a result row as an associative array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效