我正在moodle中创建一个网络服务,并希望将我的std对象作为JSON返回,
public static function add_attendance_lti($attendanceObject)
{
......
$lti_updated = [
'id' => $insert_new_attendance,
'code' => $statusCode,
'message' => $message,
'data' => json_encode($AttendanceLog)
];
return $lti_updated;
}
public static function add_attendance_lti_returns()
{
return new external_single_structure(
array(
'id' => new external_value(PARAM_RAW, 'New Attendance id'),
'code' => new external_value(PARAM_INT, 'status code of response'),
'message' => new external_value(PARAM_TEXT, 'message returned'),
'data' => new external_value(PARAM_RAW, 'Return Value')
)
);
}
在这里,我使用 json_encode() 将 $AttendanceLog 标准对象转换为 JSON,但这会返回以下数据,
{
"id": null,
"code": 200,
"message": "Attendance Record already exists",
"data": "{\"sessionid\":\"18\",\"timetaken\":1643335205,\"studentid\":\"4\",\"takenby\":\"3\",\"statusset\":\"49,51,52,50\",\"statusid\":\"52\"}"
}
如何在没有 \" 的情况下返回正常的“数据”JSON 对象