Laravel 4 JSON 输出格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20678732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:46:34  来源:igfitidea点击:

Laravel 4 JSON Output formatting

phpjsonlaravel

提问by madziikoy

return Response::json(array(
    'status' => 200,
    'posts' => $post->toArray()
), 200);

Using the code above I returned data in json format.
I have seen other api's that return json giving it back in formatted view.
Like:

使用上面的代码,我以 json 格式返回了数据。
我已经看到其他 api 返回 json 以格式化视图返回。
喜欢:

http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=440&count=3&maxlength=300&format=json

But mine is returning it in one line. How do I generate the json in a formatted way with laravel?

但我的正在一行返回它。如何使用laravel以格式化的方式生成json?



update

更新

I cannot test the code yet until I tomorrow. So I'll accept the answer tom.

直到明天我才能测试代码。所以我会接受汤姆的答案。

But this is the api

但这是api

http://laravel.com/api/class-Illuminate.Support.Facades.Response.html

and the parameters are,

和参数是,

$data
$status
$headers


update

更新

Actually I modified the response class of illuminate to have that constant.

实际上,我修改了照明的响应类以具有该常量。

回答by Travis Miller

It is possible in current 4.2 version.

在当前的 4.2 版本中是可能的。

Response::json($data=[], $status=200, $headers=[], $options=JSON_PRETTY_PRINT);

https://github.com/laravel/framework/commit/417f539803ce96eeab7b420d8b03f04959a603e9

https://github.com/laravel/framework/commit/417f539803ce96eeab7b420d8b03f04959a603e9

回答by Amal Murali

I don't think Laravel allows you to format the JSON output. However, you can do it using json_encode()'s JSON_PRETTY_PRINTconstant (available since PHP 5.4.0). Here's how:

我认为 Laravel 不允许您格式化 JSON 输出。但是,您可以使用json_encode()JSON_PRETTY_PRINT常量(自 PHP 5.4.0 起可用)来实现。就是这样:

$array = array(
    'status' => 200,
    'posts' => $post->toArray()
);

return json_encode($array, JSON_PRETTY_PRINT);

回答by Nuno Rafael Figueiredo

The same answer but with the json content-type (like the example in the question):

相同的答案,但使用 json 内容类型(如问题中的示例):

return Response::make(json_encode(array(
    'status' => 200,
    'posts' => $post->toArray()
), JSON_PRETTY_PRINT))->header('Content-Type', "application/json");

回答by XoneFobic

This is (to my knowledge) a server-side setting. Like xDebug will format it like that (also colours it).

这是(据我所知)服务器端设置。就像 xDebug 会这样格式化它(也给它上色)。

By default, JSON is a single string. And isn't related to Laravel or any other framework.

默认情况下,JSON 是单个字符串。并且与 Laravel 或任何其他框架无关。

If you're using PHP 5.4+ You could use JSON_PRETTY_PRINT

如果您使用的是 PHP 5.4+ 您可以使用 JSON_PRETTY_PRINT

return json_encode(array(
    'status' => 200,
    'posts' => $post->toArray()
), JSON_PRETTY_PRINT);

Untested and you could look in Laravel api if it's possible to use Response::json() for it.

未经测试,如果可以使用 Response::json() ,您可以查看 Laravel api。

回答by Giovanne Afonso

In Laravel 5.2 you can use a similar approach using the helpers

在 Laravel 5.2 中,您可以使用类似的方法使用 helpers

return response()->json($data=[], $status=200, $headers=[], $options=JSON_PRETTY_PRINT)