Why are JSON data enclosed in parentheses?

my JSON data returned in brackets, for example: [{"cust_id":"109"}]. Why is this? I tried mysql_fetch_rowand mysql_fetch_object. Does the substring need to remove the brackets?

Also, how can I render a JSON object in javaScript? In Firebug, if I program JSON data hard, I see the value as JSON, but alert () will not show it.

Thank.

 $rows = array();
  while($r = mysql_fetch_assoc($rs)) {
    $rows[] = $r;
  }
  echo json_encode($rows);
+5
source share
3 answers

This means an array of one element.

+5
source

Brackets indicate an array in JavaScript. If you need only one element, you need to first encode the first (and only) record of your array:

$rows = array();
while($r = mysql_fetch_assoc($rs)) 
{
    $rows[] = $r;
}
echo json_encode($rows[0]);
+4
source
+3

All Articles