开发者

JSON Data is Enclosed in Brackets Why?

开发者 https://www.devze.com 2023-02-16 00:49 出处:网络
my JSON data comes back enclosed in brackets for example: [{\"cust_id\":\"109\"}]. Why is that? I\'ve tried mysql_fetch_row and mysql_fetch_object. Do I need to substring to remove the brackets?

my JSON data comes back enclosed in brackets for example: [{"cust_id":"109"}]. Why is that? I've tried mysql_fetch_row and mysql_fetch_object. Do I need to substring to remove the brackets?

Also, how can I display a JSON object in javaScript? I开发者_运维百科n Firebug, if I hard-code the JSON data, I see a value as JSON, but alert() won't show it.

Thank you.

 $rows = array();
  while($r = mysql_fetch_assoc($rs)) {
    $rows[] = $r;
  }
  echo json_encode($rows);


That means an array consisting of excactly one element.


The brackets indicate an array in JavaScript. If you only want one item you have to encode the first (and single) entry of your array specifically:

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


Because it is JSON format syntax. More about it on: http://www.json.org/

0

精彩评论

暂无评论...
验证码 换一张
取 消