我使用codeigniter.我想从数据库中检索数据并将其转换为JSON对象而不是JSON数组.我正在使用以下代码
public function json()
{
$content = $this->db->get('todolist'); //todolist table name
$data = $content->result_array();
echo json_encode($data);
}
[{"todo_id":"1","todo_content":"Homework","date":"2016-05-05","iscomplete":null,"imagelink":"Lighthouse.jpg"},{"todo_id":"2","todo_content":"exam","date":"2015-04-21","iscomplete":null,"imagelink":"Desert.jpg"},{"todo_id":"3","todo_content":"Lab report","date":"2014-08-29","iscomplete":null,"imagelink":"FB_IMG_14700753538617403.jpg"}]
将它转换为JSON对象的最佳方法是什么
解决方法:
您可以使用JSON_FORCE_OBJECT查看下面的示例.
echo json_encode($data, JSON_FORCE_OBJECT);