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'));


任何帮助,将不胜感激 :)


摇曳的蔷薇
浏览 146回答 2
2回答

大话西游666

那是因为您必须将其作为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

临摹微笑

您使用 Guzzle 获得的结果是国家/地区列表dd($results);给array:152 [▼  "Thailand" => array:57 [▶]  "Japan" => array:57 [▶]  "Singapore" => array:57 [▶]  "Nepal" => array:57 [▶]  "Malaysia" => array:57 [▶]  "Canada" => array:57 [▶]  "Australia" => array:57 [▶]  "Cambodia" => array:57 [▶]  "Sri Lanka" => array:57 [▶]  "Germany" => array:57 [▶]  "Finland" => array:57 [▶]  ...这意味着您需要另一个循环@foreach($results as $countryData)   @foreach($countryData as $data)       {{ $data['date'] }}       {{ $data['confirmed'] }}       {{ $data['deaths'] }}       {{ $data['recovered'] }}   @endforeach@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP