php - Laravel:解析 JSON 以查看
问题描述
我正在尝试显示来自此https://pomber.github.io/covid19/timeseries.json的 JSON 数据,但出现错误:
(ErrorException(code: 0): Undefined index: confirmed
我期望的是我可以显示国家名称列表以及日期、确认等。
这是我的看法:
@foreach($results as $json_d)
{{ $json_d['date'] }}
{{ $json_d['confirmed'] }}
{{ $json_d['deaths'] }}
{{ $json_d['recovered'] }}
@endforeach
这是我的控制器:
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$results = json_decode($response, true);
return view('dashboard', compact('results'));
任何帮助,将不胜感激 :)
解决方案
那是因为您必须将其作为nested for
. 作为 Dino 的答案,但有获得国家密钥的方法。
@foreach($results as $key => $val)
Data for the country: {{ $key }}
@foreach(((array)$results)[$key] as $data)
{{ $data['date'] }}
{{ $data['confirmed'] }}
{{ $data['deaths'] }}
{{ $data['recovered'] }}
@endforeach
@endforeach
推荐阅读
- typescript - Typescript Exclude 拒绝泛型类型
- javascript - amCharts 4 上的世界地图环绕
- android - How to pass data from RecyclerView onItemClick to Activity TabLayout
- reactjs - 如何使用 React 中的功能组件按降序对数据进行排序并在表格中显示
- javascript - 使用 AJAX 向 Python 发送数据:不允许的方法 (405)
- java - 按字典顺序排序数组
- sql-server - 比较两个表并检索数据
- php - Laravel 异常属性 [product_quantity] 在 Eloquent 构建器实例上不存在
- javascript - removeEventListener - 函数定义本身内的回调?
- r - 自定义 x 轴刻度以在直方图中显示范围?