Laravel:如何在视图刀片中解析此 json 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37508277/
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 : How do I parse this json data in view blade?
提问by Danu Akbar
Hi i have JSon data like this:
嗨,我有这样的 JSon 数据:
[{label":a, "value":{"name":"abc","address"="New York"}}]
I've success print json with
我已经成功打印 json
{!! $mydata !!}
but when i try use foreach
但是当我尝试使用 foreach
@foreach($mydata['value'] as $value)
Member ID: {{ $value['name'] }}
@endforeach
The laravel return
Laravel 的回归
ErrorException in Collection.php line 1108: Undefined index:value
I've followed this : Laravel: How do I parse this json data in view blade?
我遵循了这个:Laravel:如何在视图刀片中解析这个 json 数据?
Anyone can help?
任何人都可以帮忙吗?
Note: The JSON and Variable is not real mine. I was changed it to make it simple.
注意:JSON 和变量不是我的。我改变了它以使其变得简单。
EDITED:
编辑:
added {{ dd(json_decode($tagihan, true)) }}
and return
添加{{ dd(json_decode($tagihan, true)) }}
并返回
Result after added {{ dd(json_decode($tagihan, true)) }}
添加后的结果 {{ dd(json_decode($tagihan, true)) }}
EDITED:
编辑:
This my JSON:
这是我的 JSON:
array:5 [▼
0 => array:7 [▼
"id" => 8
"no_rek_pelanggan" => 11115
"keadaan_meteran" => 0
"angka_meteran" => 3000
"created_at" => "2016-05-28 15:28:17"
"updated_at" => "2016-05-28 15:28:17"
"all_pelanggan" => array:7 [▼
"no_rek" => 11115
"nama" => "Wicak"
"alamat" => "Malang"
"long" => 112.6153447
"lat" => -7.9528411
"created_at" => "2016-05-26 00:00:00"
"updated_at" => "2016-05-26 05:00:00"
]
]
I want to read the value from nama, alamat, long, lat in all_pelanggan. How ca i do that?
我想从 all_pelanggan 中的 nama、alamat、long、lat 读取值。我怎么做?
回答by Alexey Mezenin
If you really want to use json encoded data in a view, use json_decode()in foreach
:
如果您真的想在视图中使用 json 编码数据,请在json_decode()中使用foreach
:
@foreach(json_decode($mydata, true) as $value)
Member ID: {{ $value['all_pelanggan']['name'] }}
@endforeach
回答by Mihai Matei
You didn't followed the question you provided. As stated there you should decode your json in the controller:
您没有按照您提供的问题进行操作。如上所述,您应该在控制器中解码您的 json:
return view('your-view')->with('mydata', json_decode($mydata, true));
回答by Janaka R Rajapaksha
First, you have an JSON array. But you are trying to fetch JSON object.
If you are sure there is only one object, then use in your controller Model::<selection-criteria>->first();
so that it will return a JSON object. then "your foreach" will come to work.
首先,您有一个 JSON 数组。但是您正在尝试获取 JSON 对象。如果您确定只有一个对象,则在您的控制器中使用,Model::<selection-criteria>->first();
以便它返回一个 JSON 对象。然后“你的foreach”将开始工作。
If you are not sure there is only one object or in future it will have more objects, then leave your selection statement as it is(It has ->get()
at the end). So it will return an array.
如果您不确定只有一个对象,或者将来会有更多对象,那么请保留您的选择语句(它->get()
在最后)。所以它会返回一个数组。
Now modify your foreach to fetch that array. That is your data set is an array and each element is JSON object with sub objects.
现在修改您的 foreach 以获取该数组。也就是说,您的数据集是一个数组,每个元素都是带有子对象的 JSON 对象。
@foreach($mydata as $myDatum)
@foreach($myDatum as $val)
Object label: {{ $val['label'] }}
Member Name: {{ $val['value']->name }}
Member Address: {{ $val['value']->address }}
@endforeach
@endforeach
回答by Sebas
Can you please try the following
你能试试下面的吗
@for ($i = 0; $i < count($myData); $i++)
Member ID: {{ $myData[i]['value'] }}
@endfor
I hope that works.
我希望这有效。