在单个json对象中删除[]

时间:2014-05-24 11:25:24

标签: php json

我有以下代码生成json对象。

$res=  mysql_query("SELECT * FROM `users` where `id`='$_id' ");
while ($row = mysql_fetch_object($res)) 
{
     $jsonData[] = $row;          
}
$data = json_encode($jsonData);
echo $data;

它的输出是这样的(这是json对象)。

[
   {
       "id": "11",
       "username": "tezt69",
       "insta_id": "1003817812",
       "access_token": "1003817812.9896620.aa4bbfe407f04db8b69ffff0dda60685",
       "profile_pic": "http://images.ak.instagram.com/profiles/anonymousUser.jpg",
       "coins": "9",
       "level": "1",
       "experience_pts": "0",
       "instamena": "0",
       "critical_chance": "0",
       "free_chance": "0",
       "instamena_timer": "0",
       "pause_time": "0",
       "is_promotion": "1",
       "auto_promotion": "0",
       "exp_next_level": "10",
       "media": "0",
       "follows": "0",
       "followed_by": "0",
       "added-on": "2014-05-24 02:23:09"
   }
]

现在我想删除[ ],以便它在单循环中解析。

1 个答案:

答案 0 :(得分:0)

您有几种选择。

$res=  mysql_query("SELECT * FROM `users` where `id`='$_id' ");
while ($row = mysql_fetch_object($res)) {
    $jsonData = $row;
}
$data = json_encode($jsonData);
echo $data;

OR

$res=  mysql_query("SELECT * FROM `users` where `id`='$_id' ");
while ($row = mysql_fetch_object($res)) {
    $jsonData[] = json_encode($row);
}
echo $jsonData[0];

OR

$res=  mysql_query("SELECT * FROM `users` where `id`='$_id' ");
while ($row = mysql_fetch_object($res)) {
    $jsonData[] = json_encode($row);
}
$data = $jsonData[0]
echo $data;