代码:
<?php
if(!isset($candidate_id))
{
header("location:".base_url()."login");
}
$this->db->select('event,event_title,description,s_date');
$this->db->from('event');
$this->db->where('candidate_id',$cid);
$this->db->order_by('s_date','desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result = $query->result_array();
$record = array();
foreach($result as $row)
{
$record[] = $row;
}
echo json_encode($record,JSON_NUMERIC_CHECK);
}
else
{
$this->session->set_flashdata('no_event',"<p>No event Added</p>");
}
?>
当前输出:
[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]
预期输出:
[{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"}]
我正在使用json_encode()函数创建一个简单的JSON API。现在,我成功创建了API,但是如上所述,输出是意外的。现在,我在上面实际上也想要提到。那么,如何获得预期的输出?请帮助我。
谢谢
答案 0 :(得分:1)
问题是,如果您从键中删除引号,则它不再是JSON。我假设您希望它类似于JavaScript对象,但这应该在客户端使用JSON.parse()完成。
即:
var apiResponse = '[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]';
var json = JSON.parse(apiResponse);
console.log(json);
console.log(json[0].description);
答案 1 :(得分:-1)
尝试一下
$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',json_encode($you-array));
输出
{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"}