Php 如何在laravel中返回响应对象json
我已经得到了回应,但我不需要这样Php 如何在laravel中返回响应对象json,php,json,laravel,laravel-5,Php,Json,Laravel,Laravel 5,我已经得到了回应,但我不需要这样 { "status": "200", "id": 1, "email": "a@a.a", "mobile": "", "source": "", "source_id": "", "message": "Bad Request : Already Logged In" } 我需要返回另一个对象中的数据,如: { { "status": "200", } { "id": 1, "email": "a@a.a",
{
"status": "200",
"id": 1,
"email": "a@a.a",
"mobile": "",
"source": "",
"source_id": "",
"message": "Bad Request : Already Logged In"
}
我需要返回另一个对象中的数据,如:
{
{
"status": "200",
}
{
"id": 1,
"email": "a@a.a",
"mobile": "",
"source": "",
"source_id": "",
"message": "Bad Request : Already Logged In"
}
}
2021年2月9日更新:如果适用,将模型序列化为JSON的最佳方法是使用。这在构建RESTful API时特别有用
使用response和json方法
这将返回以下JSON:
{
"status": "200",
"details": {
"id": 1,
"email": "a@a.a",
"mobile": "",
"source": "",
"source_id": "",
"message": "Bad Request : Already Logged In"
}
}
只需返回数组:
$data = [
"status" => "200",
"details" => [
"id": 1,
"email": "a@a.a",
"mobile": "",
"source": "",
"source_id": "",
"message": "Bad Request : Already Logged In"
]
];
return $data;
它将被Laravel解析为JSON响应:
[…]您还可以返回数组。框架将自动将数组转换为JSON响应
只需返回数组json
return response()->json(['status'=>200,'data'=>$products]);
"status": 200,
"data": [
{
"id": 15,
"title": {
"en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
"ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
"ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
},
"slug": {
"en": "someone-s-sitting-in-the-shade-today-because-someone-planted-a-tree-a-long-time-ago",
"ar": "شخص-ما-يجلس-في-الظل-اليوم-لأن-أحدهم-زرع-شجرة-منذ-زمن-بعيد",
"ca": "alguien-esta-sentado-a-la-sombra-hoy-porque-alguien-planto-un-arbol-hace-mucho-tiempo"
},
"description": {
"en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
"ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
"ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
},
{
"id": 14,
"title": {
"en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
"ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
"ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
},
"slug": {
"en": "define-success-on-your-own-terms-achieve-it-by-your-own-rules-and-build-a-life-you-re-proud-to-live",
"ar": "حدد-النجاح-بشروطك-الخاصة-وتحققه-بقواعدك-الخاصة-واصنع-حياة-تفخر-بأن-تعيشها",
"ca": "defina-el-exito-en-sus-propios-terminos-consigalo-segun-sus-propias-reglas-y-construya-una-vida-de-la-que-se-sienta-orgulloso-de-vivir"
},
"description": {
"en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
"ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
"ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
},
{
"id": 13,
"title": {
"en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
"ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
"ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
},
"slug": {
"en": "never-give-up-today-is-hard-tomorrow-will-be-worse-but-the-day-after-tomorrow-will-be-sunshine",
"ar": "لا-تستسلم-أبدا-اليوم-صعب-غدا-سيكون-أسوأ-لكن-بعد-غد-سيكون-شروق-الشمس",
"ca": "nunca-te-rindas-hoy-es-duro-manana-sera-peor-pero-pasado-manana-habra-sol"
},
"description": {
"en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
"ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
"ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
}
]有什么问题吗?到目前为止您尝试了什么?第二个是无效的json格式。我不这么认为,第一个在解析方面有任何问题。但是您使用的格式是{status:200,details:{id:1,email:a@a.a,mobile:,source:,source_id:,message:Bad Request:ready Logged In}我已经得到了这个解决方案,但是android开发者仍然存在问题,他不需要像细节、数据之类的东西。他需要一个直接包含{id:1,email:a@a.com)我希望你能理解我,谢谢s@EsLaM虽然没有直接回答,但让我宣传一下我不久前为Laravel创建的ResponseBuilder包,以规范化和简化使用L编写API:虽然这个代码片段可能是解决方案,确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
$data = [
"status" => "200",
"details" => [
"id": 1,
"email": "a@a.a",
"mobile": "",
"source": "",
"source_id": "",
"message": "Bad Request : Already Logged In"
]
];
return $data;
return response()->json(['status'=>200,'data'=>$products]);
"status": 200,
"data": [
{
"id": 15,
"title": {
"en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
"ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
"ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
},
"slug": {
"en": "someone-s-sitting-in-the-shade-today-because-someone-planted-a-tree-a-long-time-ago",
"ar": "شخص-ما-يجلس-في-الظل-اليوم-لأن-أحدهم-زرع-شجرة-منذ-زمن-بعيد",
"ca": "alguien-esta-sentado-a-la-sombra-hoy-porque-alguien-planto-un-arbol-hace-mucho-tiempo"
},
"description": {
"en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
"ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
"ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
},
{
"id": 14,
"title": {
"en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
"ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
"ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
},
"slug": {
"en": "define-success-on-your-own-terms-achieve-it-by-your-own-rules-and-build-a-life-you-re-proud-to-live",
"ar": "حدد-النجاح-بشروطك-الخاصة-وتحققه-بقواعدك-الخاصة-واصنع-حياة-تفخر-بأن-تعيشها",
"ca": "defina-el-exito-en-sus-propios-terminos-consigalo-segun-sus-propias-reglas-y-construya-una-vida-de-la-que-se-sienta-orgulloso-de-vivir"
},
"description": {
"en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
"ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
"ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
},
{
"id": 13,
"title": {
"en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
"ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
"ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
},
"slug": {
"en": "never-give-up-today-is-hard-tomorrow-will-be-worse-but-the-day-after-tomorrow-will-be-sunshine",
"ar": "لا-تستسلم-أبدا-اليوم-صعب-غدا-سيكون-أسوأ-لكن-بعد-غد-سيكون-شروق-الشمس",
"ca": "nunca-te-rindas-hoy-es-duro-manana-sera-peor-pero-pasado-manana-habra-sol"
},
"description": {
"en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
"ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
"ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
},
"created_at": "2021-03-15T18:31:02.000000Z",
"updated_at": "2021-03-15T18:31:02.000000Z"
}