【发布时间】:2017-04-19 06:10:35
【问题描述】:
我正在使用 Codeigniter RESTful API 根据输入从数据库中获取数据。现在我想改变以不同方式显示我的 json 输出的方式。
代码:
function categoryskills_get()
{
$category = $this->get('category');
$skills = $this->Users_model->categoryskills($category);
if($skills != FALSE)
{
$sub_cat = 0;
foreach($skills['skills'] as $row)
{
$data[$skills['cat']][]['sub_category'] = $row['sub_cat'];
$data[$skills['cat']][]['skills'] = $row['s_name'];
}
$this->set_response($data, REST_Controller::HTTP_OK);
}else{
$response["error"] = TRUE;
$response["status"] = '404';
$response["error_msg"] = "Category not found!";
$this->set_response($response, REST_Controller::HTTP_NOT_FOUND);
}
}
上面的代码是我的 http 请求的控制器。
电流输出:
{
"Consultants": [
{
"sub_category": "Consultants"
},
{
"skills": "Career Counsellor, Creative Consultant,Digital Consultant"
},
{
"sub_category": "Accounting"
},
{
"skills": "Accountant,Auditor,Tax Specialist"
}
]
}
预期输出:
{
"Consultants": [
{
"sub_category": "Consultants",
"skills": "Career Counsellor,Creative Consultant,Digital Consultant"
},
{
.....
}
]
}
【问题讨论】:
标签: json codeigniter api encoding