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

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据