如何使用 Laravel 获取这种格式的 JSON 数据?

普拉莫德·亚达夫

我尝试将数据保存到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",
      }
   ]
}

这是我的 MySql 数据库 在此处输入图片说明

请建议我如何以我想要的格式获取我的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章