Laravel - 将字段从数组传递到 JSON

Laravel - Pass fields from array to JSON

嗨,我有一个对象数组,我想将其传递给 laravel 到 json,我的数组中有两个值,我希望 json 通过这是浏览器名称和使用百分比。

这可能有很多结果,所以我首先计算字段,然后遍历这些字段:

$index = $this->client->fetchPages($id);

$browsergraphs=array();
    // loop through repeated block by UID
    $count = 0 + ($index->fields->{"browserstats"});
    for ($x = 0; $x < $count; $x++) {
        $browsergraphs[$x] = [
        'browsername' => $index->fields->{"browsername-repeated-block-".$x},
        'usagepercentage' => $index->fields->{"usagepercentage-repeated-block-".$x},
        ];
    }

可能值得一提的是,我使用的是自定义 API 因此调用 $this->client->fetchPages($id);

现在如果我dd($browsergraphs);

我的输出是:

array(5) { [0]=> array(2) { ["browsername"]=> string(2) "IE" ["usagepercentage"]=> string(4) "76.2" } [1]=> array(2) { ["browsername"]=> string(7) "Mozilla" ["usagepercentage"]=> string(4) "16.5" } [2]=> array(2) { ["browsername"]=> string(8) "Netscape" ["usagepercentage"]=> string(3) "1.7" } [3]=> array(2) { ["browsername"]=> string(5) "Opera" ["usagepercentage"]=> string(3) "1.6" } [4]=> array(2) { ["browsername"]=> string(5) "Other" ["usagepercentage"]=> string(1) "4" } }

但是如果我执行以下操作:

   $jsongraphs = Response::json($browsergraphs);
        dd($jsongraphs);

我的输出如下:

object(Illuminate\Http\JsonResponse)#127 (10) { ["jsonOptions":protected]=> int(0) ["data":protected]=> string(243) "[{"browsername":"IE","usagepercentage":"76.2"},{"browsername":"Mozilla","usagepercentage":"16.5"},{"browsername":"Netscape","usagepercentage":"1.7"},{"browsername":"Opera","usagepercentage":"1.6"},{"browsername":"Other","usagepercentage":"4"}]" ["callback":protected]=> NULL ["encodingOptions":protected]=> int(15) ["headers"]=> object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#166 (5) { ["computedCacheControl":protected]=> array(1) { ["no-cache"]=> bool(true) } ["cookies":protected]=> array(0) { } ["headerNames":protected]=> array(3) { ["cache-control"]=> string(13) "Cache-Control" ["date"]=> string(4) "Date" ["content-type"]=> string(12) "Content-Type" } ["headers":protected]=> array(3) { ["cache-control"]=> array(1) { [0]=> string(8) "no-cache" } ["date"]=> array(1) { [0]=> string(29) "Fri, 16 Jan 2015 14:55:08 GMT" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } } ["cacheControl":protected]=> array(0) { } } ["content":protected]=> string(243) "[{"browsername":"IE","usagepercentage":"76.2"},{"browsername":"Mozilla","usagepercentage":"16.5"},{"browsername":"Netscape","usagepercentage":"1.7"},{"browsername":"Opera","usagepercentage":"1.6"},{"browsername":"Other","usagepercentage":"4"}]" ["version":protected]=> string(3) "1.0" ["statusCode":protected]=> int(200) ["statusText":protected]=> string(2) "OK" ["charset":protected]=> NULL }

我认为这不是我需要的,这看起来也不像 json,我做错了什么吗?

我正在尝试通过 json 传递浏览器名称和使用百分比,以填充 google 饼图。

知道如何通过 json 发送这两个值吗?

Response::json 不只是将数据转换成 JSON 它会创建一个 JsonResponse object,当从您的控制器操作返回时,设置响应 headers 和 returns JSON 到客户端。

如果您只想将数组转换为 json,请执行以下操作:

$json = json_encode($browsergraphs)

$jsongraphs = Response::json($browsergraphs); dd($jsongraphs->getData());