【问题标题】:Laravel JSON maxCDN / Chrome JSON Formatter does not workLaravel JSON maxCDN / Chrome JSON 格式化程序不起作用
【发布时间】:2015-08-15 23:01:41
【问题描述】:

目前我正在使用 Laravel 和 maxcdn API。 这是我很小的测试代码

Route::get('cdn', function() {

    $api = new MaxCDN("myurl","mykey","mysecret");
    $data = $api->get('/account.json');
    return response()->json($data);

});

如你所见,标头设置为 json

application/json

现在,如果我访问我的 /cdn url,Chrome JSON 格式化程序当然应该格式化输出。但它不起作用,我只得到这个输出

"{\"code\":200,\"data\":{\"account\":{\"id\":\"12312\",\"name\":\"My Name \",\"alias\":\"myurl\",\"date_created\":\"2015-05-17 07:51:50\",\"date_updated\":\"0000-00-00 ........

我正在研究我的宅基地机器

php -v PHP 5.6.6-1+deb.sury.org~utopic+1 (cli) (built: Feb 20 2015 11:25:37) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

【问题讨论】:

  • 你好。我在 MaxCDN 工作,我有一个认识 Larvel 的朋友。坚持住,我会让他帮你解决的。
  • @jdorfman 会很棒:)

标签: php json google-chrome laravel


【解决方案1】:

$api->get('/account.json'); 调用返回一个包含 json 数据的字符串。然后,您将此字符串传递给 response()->json(); 方法,这就是您看到的输出。

您需要通过json_decode() 运行字符串,以便将json 字符串转换为PHP 对象。完成此操作后,您可以将该结果传递给 response()->json(); 方法,您将获得所需的结果。

Route::get('cdn', function() {
    $api = new MaxCDN("myurl","mykey","mysecret");
    $stringData = $api->get('/account.json');

    // convert the json string into an actual PHP object
    $data = json_decode($stringData);

    // respond with the object
    return response()->json($data);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    相关资源
    最近更新 更多