PHP-запрос к данным для Android

У меня проблемы с преобразованием PHP-запроса в данные json, которые можно прочитать на Android.

Я следовал этому руководству для получения данных JSON:
http://www.androidhive.info
/ 2012/01 / андроид-JSON-синтаксический-учебник /

Я пытаюсь преобразовать этот запрос 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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } } ] }

Что я делаю неправильно?

0

Решение

Похоже, вы пропустили закрывающую скобку в своем эхо-заявлении.
Чтобы избежать подобных ошибок, вы хотите построить весь объект перед его кодированием следующим образом:

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

Другие решения

Эта строка неверна

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

Попробуйте вот так.

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

Снимите последнюю скобку, чтобы она стала следующей:

{"contacts":[{"id":"1 ","name":"Michael ","email":"[email protected]","address":"a ","gender":"b ","phone":"c ","mobile":"d ","home":"e ","office":"f "}]}
0
По вопросам рекламы [email protected]