Page 1 of 1

Json formats

Posted: Tue Sep 08, 2015 11:00 am
by adityagis
json1.png
json1.png (10.54 KiB) Viewed 748 times
Hi Team,
I need to create a JSON from my php. As of now I am able to get the JSON, but it is needed in a different format.
Mentioned below is the code I am using

<?php
$connectString = 'host=localhost dbname=postgis_21_sample user=postgres password=postgresql';
$link = pg_connect($connectString);

if (!$link) {
echo "error";
} else {

$result = pg_query($link, $sql);

//$rows = array();
while($r = pg_fetch_assoc($result)) {
$rows[] = $r;
}

print json_encode($rows,true);
}
?>

json.png
Attached is the image json.png which show how the following code shows the output.

json1.png
json1.png (10.54 KiB) Viewed 748 times
Attached is the image of the required format.

Please help me to get the desired format.


Thanks
Aditya Kumar

Re: Json formats

Posted: Tue Sep 08, 2015 9:57 pm
by Christopher
Try the JSON_FORCE_OBJECT flag.