【问题标题】:PHP query to android readable dataPHP查询到android可读数据
【发布时间】:2014-10-28 18:50:44
【问题描述】:

我在将 PHP 查询转换为 android 可读 json 数据时遇到问题。

我按照本教程检索 json 数据: http://www.androidhive.info /2012/01/android-json-parsing-tutorial/

我正在尝试将此 PHP 查询转换为 json 数据:

<?php
include('db.php');
$sql=sqlsrv_query($conn,"select * FROM butler");

$result = array();
while($row=sqlsrv_fetch_object($sql))
{
$result[]=$row;

}
echo '{"contacts":'.json_encode($result);

sqlsrv_close($conn);
?>

浏览器中的输出是这样的,但是在android上它显示空白屏幕:

    {"contacts":[{"id":"1 ","name":"Michael ","email":"guitarda ","address":"a ","gender":"b ","phone":"c ","mobile":"d ","home":"e ","office":"f "}]}}

这是来自 json 教程的有效数据:

 { "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c201", "name": "Johnny Depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c202", "name": "Leonardo Dicaprio", "email": "leonardo_dicaprio@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c203", "name": "John Wayne", "email": "john_wayne@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c204", "name": "Angelina Jolie", "email": "angelina_jolie@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c205", "name": "Dido", "email": "dido@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c206", "name": "Adele", "email": "adele@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c207", "name": "Hugh Jackman", "email": "hugh_jackman@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c208", "name": "Will Smith", "email": "will_smith@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c209", "name": "Clint Eastwood", "email": "clint_eastwood@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2010", "name": "Barack Obama", "email": "barack_obama@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2011", "name": "Kate Winslet", "email": "kate_winslet@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2012", "name": "Eminem", "email": "eminem@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } } ] }

我做错了什么?

【问题讨论】:

标签: php android sql json listview


【解决方案1】:

您似乎错过了 echo 语句中的右括号。 为了避免这样的错误,你想在编码之前构造整个对象:

echo json_encode(array(
    "contacts" => $result
));

【讨论】:

    【解决方案2】:

    这行写错了

    echo '{"contacts":'.json_encode($result); 
    

    像这样试试。

    $data["contacts"] = $result; // outside for loop
    echo  json_encode($data);
    

    【讨论】:

      【解决方案3】:

      去掉最后一个括号,使其变成如下:

      {"contacts":[{"id":"1 ","name":"Michael ","email":"guitarda@xyz.zzz","address":"a ","gender":"b ","phone":"c ","mobile":"d ","home":"e ","office":"f "}]}
      

      【讨论】:

        猜你喜欢
        • 2020-09-08
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2018-10-05
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-01
        相关资源
        最近更新 更多