繁体   English   中英

api响应中json或xml的可读视图。 Yii2

[英]Readable view of json or xml in api response. Yii2

在指南中,以清晰的视图显示api的响应。 我想了解一下,它是仅用于显示响应的结构吗,还是有一种使视图更易读的方法。

终端中的示例:我的代码:响应

"date_imported":"2016-03-19 18:30:22"}],"_links":{"self":{"href":"http://localhost/tweets?page=1"},"next":{"href":"http://localhost/tweets?page=2"},"last":{"href":"http://localhost/tweets?page=23"}},"_meta":{"totalCount":450,"pageCount":23,"currentPage":1,"perPage":20}}

docs:以相同的代码回复

"_links": {
        "self": {
            "href": "http://localhost/users?page=1"
        },
        "next": {
            "href": "http://localhost/users?page=2"
        },
        "last": {
            "href": "http://localhost/users?page=50"
        }
    },
    "_meta": {
        "totalCount": 1000,
        "pageCount": 50,
        "currentPage": 1,
        "perPage": 20
    }
}

是仅用于演示,并且实际响应具有字符串视图,还是我做错了?

如果您确实想要“可读的” json响应,则只需修改yii\\web\\JsonResponseFormatter $prettyPrint属性:

$prettyPrint (自版本2.0.7起可用):是否以可读的“漂亮”格式格式化输出。

要配置此属性,可以配置response应用程序组件,如下所示:

'response' => [
    'formatters' => [
        \yii\web\Response::FORMAT_JSON => [
             'class' => 'yii\web\JsonResponseFormatter',
             'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode
        ],
    ],
],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM