我很难从JSON响应中获取数据。 这是我获取数据的代码:
$client = new Client;
$r = $client->get("http://my.api.com/get-campaign/" . $id . "?api_token=1235");
$apiResult = json_decode($r->getBody(), true);
dd($apiResult);
我得到这样的东西:
array:4 [
"campaign" => array:1 [
0 => array:3 [
"manufacturer" => "Sony"
"product" => "PlayStation 4"
"created_at" => "2015-07-04T00:00:00+00:00"
]
]
"media" => array:2 [
"video" => "https://my.domain.com/421156.mp4"
"images" => "https://my.domain.com/tv/thumbs/421156-1.jpg"
]
"statistics" => array:3 [
"runs" => 172
"firstseen_at" => "2015-07-04T19:06:41+00:00"
"lastseen_at" => "2015-07-09T12:04:13+00:00"
]
"broadcasts" => array:172 []
]
如何从此响应中获取单个值? 假设我想显示,或者为另一个变量分配"manufacturer"
的值,并在另一个变量中存储运行次数( "runs"
)?
对于制造商,我试图做这样的事情:
dd($apiResult["campaign"]->manufacturer);
但随后显示错误 - 试图获取非属性对象!