dounangshen6553 2016-09-24 09:17
浏览 64
已采纳

将result_array()转换为JSON对象

What i am trying to do is convert result_array() given by my query and directly convert it into JSON object , which can further be set in controller like this:

$this->response($jsonobject, REST_Controller:: HTTP_OK);

my attempt by studying several SO questions :

$this->response(json_encode($result_array), REST_Controller:: HTTP_OK);

but my attempt is wrong i think because it return json itself in form of string , how to achieve this then?

Example:

$pakistan = array('status' => 'OK','message' => 'yes i am ok');
// convert pakistan to something like
$pakistan = [ 'status' => 'OK','message' => 'yes i am ok' ];
  • 写回答

1条回答 默认 最新

  • dongli5785 2016-09-24 09:25
    关注

    Try to return the query result not result_array

    model function

    function getRecords(){
       $sql = 'SELECT * FROM table';
       $query = $this->db->query($sql);
       return $query->result();
    }
    

    Convert to json

    $object = getRecords();
    $json_obj = json_encode($object);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?