我尝试将数据保存到MySQL数据库并成功保存数据。我想以这种格式获取 JSON 数据
{
"response_code":0,
"results":[
{
"question":"Here is my question?",
"correct_answer":"Correct Answer",
"incorrect_answers":[
"wrong_1",
"wrong_2",
"wrong_3"
]
}
]
}
但我以这种格式获取我的 JSON 数据
{
"response_code":0,
"results":[
{
"question":"Here is my question?",
"correct_answer":"right_ans",
"incorrect_answers_1":"wrong_1",
"incorrect_answers_2":"wrong_2",
"incorrect_answers_3":"wrong_3",
}
]
}
请建议我如何以我想要的格式获取我的 JSON 数据?如果您想要更多文件或信息,那么我将编辑我的问题。
public function toArray($request)
{
return [
"id"=>$this->id,
"right_ans" =>$this->right_ans,
"incorrect_answers_1" =>$this->wrong_1,
"incorrect_answers_1" =>$this->wrong_2,
"incorrect_answers_1" =>$this->wrong_3,
"created"=>$this->created_at,
];
}
尝试创建这样的数组。它将按预期返回 json。
public function toArray($request)
{
return [
"id"=>$this->id,
"right_ans" =>$this->right_ans,
"incorrect_answers" => array (
$this->wrong_1,
$this->wrong_2,
$this->wrong_3),
"created"=>$this->created_at,
];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句