0

I'm having problems converting a PHP query to android readable json data.

I followed this tutorial for retrieving json data: http://www.androidhive.info /2012/01/android-json-parsing-tutorial/

I'm trying to convert this PHP query to json data:

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

The output in the browser looks like this, but on android it shows blank screen:

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

This is the data from the json tutorial which works:

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

What am I doing wrong?

user3888775
  • 113
  • 3
  • 16

3 Answers3

0

It looks like you missed the closing bracket in your echo statement. To avoid mistakes like this, you want to construct the whole object before encoding it like so:

echo json_encode(array(
    "contacts" => $result
));
paquino
  • 464
  • 2
  • 5
0

Remove the last bracket so that it becomes the following:

{"contacts":[{"id":"1 ","name":"Michael ","email":"guitarda@xyz.zzz","address":"a ","gender":"b ","phone":"c ","mobile":"d ","home":"e ","office":"f "}]}
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
Skywalker
  • 1,717
  • 1
  • 22
  • 25
0

This line is wrong

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

Try Like this.

$data["contacts"] = $result; // outside for loop
echo  json_encode($data);
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44