Laravel - 将字段从数组传递到 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27986795/
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
Laravel - Pass fields from array to JSON
提问by 001221
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.
嗨,我有一个对象数组,我想将它们传递给 json,我的数组中有两个值,我希望 json 传递它们,分别是 browsername 和 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);
$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},
];
}
It's possibly worth mentioning that I am using a custom API hence the call $this->client->fetchPages($id);
可能值得一提的是,我正在使用自定义 API,因此调用 $this->client->fetchPages($id);
Now if I dd($browsergraphs);
现在如果我 dd($browsergraphs);
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);
dd($jsongraphs);
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?
我认为这不是我所需要的,这看起来不像 json,是不是我做错了什么?
I'm trying to pass the browsername and usagepercentage via json in order to populate a google pie chart.
我正在尝试通过 json 传递浏览器名称和使用百分比以填充谷歌饼图。
Any ideas how I can send those two values via json?
任何想法如何通过 json 发送这两个值?
回答by lukasgeiter
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.
Response::json
不只是将数据转换为 JSON,它会创建一个JsonResponse
对象,当从控制器操作返回时,设置响应头并将 JSON 返回给客户端。
If you just want to convert your array to json, do:
如果您只想将数组转换为 json,请执行以下操作:
$json = json_encode($browsergraphs)
回答by Manikanta Siripella
$jsongraphs = Response::json($browsergraphs); dd($jsongraphs->getData());
$jsongraphs = Response::json($browsergraphs); dd($jsongraphs->getData());