如何将 json 文件加载到 Laravel 控制器中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42652715/
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
How to load json file into Laravel Controller?
提问by Zach Tackett
I have a JSON file that I would like to load into a controller in Laravel so that I can use the data into my application.
我有一个 JSON 文件,我想将它加载到 Laravel 中的控制器中,以便我可以将数据用于我的应用程序。
The files path is /storage/app/calendar_Ids.json.
文件路径为 /storage/app/calendar_Ids.json。
What is the correct way to go about doing this?
这样做的正确方法是什么?
回答by Mike Barwick
Here, this should help you get sorted.
在这里,这应该可以帮助您进行排序。
use Storage;
$json = Storage::disk('local')->get('calendar_Ids.json');
$json = json_decode($json, true);
回答by Onix
Try this
尝试这个
$path = '/storage/app/calendar_Ids.json';
$content = json_decode(file_get_contents($path), true);`
Then just dd($content);
to see if it works.
然后只是dd($content);
看看它是否有效。
回答by Yannick Dirbé
You can try this too:
你也可以试试这个:
$json = storage_path('folder/filename.json');