在 Yii2 中以 JSON 格式获取响应
Get response in JSON format in Yii2
我正在尝试将响应数组转换为 JSON 格式。我已经尝试了在 SO 和其他网站上发布的所有答案,例如 web1,web2 添加 header('Content-Type: application/json')
然后 echo json_encode($data,JSON_PRETTY_PRINT);
但我总是得到文本格式的输出。
谁能帮我解决这个问题。
助手Class:
public static function renderJSON($data) {
header('Content-Type: application/json');
echo json_encode($data,JSON_PRETTY_PRINT);
}
我的控制器:
if ($model->login()) {
$user = User::findByUsernameOrEmail($request->post('username'));
$userArray = ArrayHelper::toArray($user);
Helpers::renderJSON($userArray);
我尝试打印 userArray
,它看起来像这样:
Array
(
[name] => abc
[lastname] => xyz
[username] => test_test
)
Json 输出:(html/text)
{
"name": "abc",
"lastname": "xyz",
"username": "test_test"
}
设置
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
在 return
之前某处的控制器操作中。
只需在控制器中添加这个
public function beforeAction($action)
{
\Yii::$app->response->format = Response::FORMAT_JSON;
return parent::beforeAction($action);
}
从 Yii 2.0.11 开始,有一个专用的 asJson()
方法来 return JSON 格式的响应。 运行:
return $this->asJson($array);
在您的控制器操作中。
use yii\helpers\Json;
use yii\web\Response;
首先在您的控制器顶部包含上面的两行,然后在您的任何控制器操作中,就在 return 语句之前,包含下面的
Yii::$app->response->format = Response::FORMAT_JSON;
return Json::encode([
'message' => 'success'
]);
您可以根据需要构建 return 数组。
use JsonException;
use yii\web\NotFoundHttpException;
use yii\base\InvalidConfigException;
use yii\web\Response;
use yii\web\JsonResponseFormatter;
/**
* @param int $id
* @return string[]
* @throws InvalidConfigException
* @throws JsonException
* @throws NotFoundHttpException
*/
public function actionViewJson(): array
{
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->formatters = [
Response::FORMAT_JSON => [
'class' => JsonResponseFormatter::class,
'prettyPrint' => true
]
];
return ['key' => 'val'];
}
我正在尝试将响应数组转换为 JSON 格式。我已经尝试了在 SO 和其他网站上发布的所有答案,例如 web1,web2 添加 header('Content-Type: application/json')
然后 echo json_encode($data,JSON_PRETTY_PRINT);
但我总是得到文本格式的输出。
谁能帮我解决这个问题。
助手Class:
public static function renderJSON($data) {
header('Content-Type: application/json');
echo json_encode($data,JSON_PRETTY_PRINT);
}
我的控制器:
if ($model->login()) {
$user = User::findByUsernameOrEmail($request->post('username'));
$userArray = ArrayHelper::toArray($user);
Helpers::renderJSON($userArray);
我尝试打印 userArray
,它看起来像这样:
Array
(
[name] => abc
[lastname] => xyz
[username] => test_test
)
Json 输出:(html/text)
{
"name": "abc",
"lastname": "xyz",
"username": "test_test"
}
设置
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
在 return
之前某处的控制器操作中。
只需在控制器中添加这个
public function beforeAction($action)
{
\Yii::$app->response->format = Response::FORMAT_JSON;
return parent::beforeAction($action);
}
从 Yii 2.0.11 开始,有一个专用的 asJson()
方法来 return JSON 格式的响应。 运行:
return $this->asJson($array);
在您的控制器操作中。
use yii\helpers\Json;
use yii\web\Response;
首先在您的控制器顶部包含上面的两行,然后在您的任何控制器操作中,就在 return 语句之前,包含下面的
Yii::$app->response->format = Response::FORMAT_JSON;
return Json::encode([
'message' => 'success'
]);
您可以根据需要构建 return 数组。
use JsonException;
use yii\web\NotFoundHttpException;
use yii\base\InvalidConfigException;
use yii\web\Response;
use yii\web\JsonResponseFormatter;
/**
* @param int $id
* @return string[]
* @throws InvalidConfigException
* @throws JsonException
* @throws NotFoundHttpException
*/
public function actionViewJson(): array
{
Yii::$app->response->format = Response::FORMAT_JSON;
Yii::$app->response->formatters = [
Response::FORMAT_JSON => [
'class' => JsonResponseFormatter::class,
'prettyPrint' => true
]
];
return ['key' => 'val'];
}