检索到json冗余数据

时间:2011-05-08 21:50:00

标签: php jquery json

$('.editBtn').click(function(){//EDIT BUTTON EVENT
        $.getJSON('edit.php?url='+encodeURI($(this).siblings('a').attr('id'))+'&action=edit',function(data){
           $.each(data, function(key, val) {
               alert(key+': '+val);
            });
        });
    });//EDIT BUTTON END

这里是php的问题:

elseif($_GET['action']=='edit'){
$output=$mysql->getDb()->query("select * from video
    where url='{$_GET['url']}'")->fetchAll();
header("content-type: application/json");
echo json_encode($output[0]);
}

当点击.editBtn按钮时,会弹出0:value0, 1:value1,...,然后再次进入,但我只希望它为name0:value0, name1:value1,...

发生了什么事?

P.S。 php独立运行:

{"url":"www.vimeo.com\/20721308","0":"www.vimeo.com\/20721308","title":"Dis-patch Festival R.I.P.","1":"Dis-patch Festival R.I.P.","description":"Sadly, the last goodbyes to the Dis-patch Festival Belgrade edition in this tribute \"R.I.P.\" video collage. The end is always the beginning...","2":"Sadly, the last goodbyes to the Dis-patch Festival Belgrade edition in this tribute \"R.I.P.\" video collage. The end is always the beginning...","country":"serbia","3":"serbia","postDate":"2011-05-07 05:56:04","4":"2011-05-07 05:56:04","views":null,"5":null}

1 个答案:

答案 0 :(得分:0)

不完全确定这里返回的格式。通常框架SQL对象以它自己的对象格式返回数据集。您可能希望将SQL数据传递给数组对象,然后将数组json_encode返回给您客户端。

$returnData = array(
    'name' => $output[0]->name,
    'videopath' => $output[1]->videopath 
);

echo json_encode(array('success'=>1, 'data'=>$returnData));