【发布时间】:2021-08-30 04:12:20
【问题描述】:
我正在尝试从互联网上找到的外部 API 读取数据,我在互联网上关注了几篇文章,但仍然存在如下错误,有谁知道我如何从JSON?
错误:
ErrorException {#196 ▼
#message: "Illegal string offset 'verses'"
#code: 0
#file: "C:\laragon\www\distrik24blog\storage\framework\views\e523d302202180142edce531702b14dc31954a07.php"
#line: 42
#severity: E_WARNING
}
控制器代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class alkitabcontroller extends Controller
{
public function fetchInitialData(){
$client = new Client;
$response = $client->get("https://api-alkitab.herokuapp.com/v2/passage/kejadiam/1?ver=tb");
$json_string = $response->getBody()->getContents();
$data = json_decode($json_string);
return view('khotbahdanrenungan/alkitab', compact('data'));
}
public function getDataAlkitab(Request $request){
$kitab = $request->input('kitab');
$pasal = $request->input('pasal');
$client = new Client;
$response = $client->get("https://api-alkitab.herokuapp.com/v2/passage/$kitab/$pasal?ver=tb");
$json_string = $response->getBody()->getContents();
$data = json_decode($json_string);
return view('khotbahdanrenungan/alkitab', compact('data'));
}
}
网络:
Route::get('/alkitab','alkitabcontroller@fetchInitialData');
Route::post('/searchdata','alkitabcontroller@getDataAlkitab');
刀片代码:
<div class="col-md-9">
<div class="card">
<div class="card-body">
@foreach ($data as $item)
<label for="kitab"><span>Markus</span><span>24</span></label>
<p>{{ $item['verses']['content']}}</p>
<hr>
@endforeach
</div>
</div>
</div>
json 结构:
{
"title": "Yohanes 1:1-51",
"book_number": 43,
"chapter": 1,
"verses": [
{
"verse": 1,
"content": "Pada mulanya adalah Firman; Firman itu bersama-sama dengan Allah dan Firman itu adalah Allah."
},
{
"verse": 2,
"content": "Ia pada mulanya bersama-sama dengan Allah."
},
【问题讨论】: