001221 001221 - 1 year ago 136
JSON Question

Laravel - Pass fields from array to JSON

Hi I have an array of objects, which I want to pass in laravel to json, I have two values within my array that I want the json to pass through which is browsername and usagepercentage.

This can have a number of results so firstly I count the fields and then iterate through these as so:

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

// 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},

It’s possibly worth mentioning that I am using a custom API hence the call

Now if I

My output is:

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" } }

But if I do the following:

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

My output is as follows:

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 }

Which I don’t think is what I require and this doesn’t look like json, is there something I have done wrong?

I’m trying to pass the browsername and usagepercentage via json in order to populate a google pie chart.

Any ideas how I can send those two values via json?

Answer Source

Response::json doesn't just convert the data into JSON it creates a JsonResponse object, that when returned from your controller action, sets response headers and returns the JSON to the client.

If you just want to convert your array to json, do:

$json = json_encode($browsergraphs)