dongyu4908
2017-07-10 09:17
浏览 232
已采纳

如何使用Guzzle和Laravel从JSON响应中提取单个数组值

I'm struggling to get data from JSON response. This is my code for getting the data:

$client = new Client;
$r = $client->get("http://my.api.com/get-campaign/" . $id . "?api_token=1235");
$apiResult = json_decode($r->getBody(), true);

dd($apiResult);

And I get something like this:

    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 []
]

How can I get single values from this response? Let's say I want to display, or assign to another variable the value of "manufacturer" and in another variable to store number of runs ("runs")?

For manufacturer I've tried to do something like this:

dd($apiResult["campaign"]->manufacturer);

But then error is shown - trying to get non-property object!

图片转代码服务由CSDN问答提供 功能建议

我很难从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); 
 <  / code>  
 
 

我得到这样的东西:

  array:4 [
“campaign”=&gt; 数组:1 [
 0 =&gt; 数组:3 [
“制造商”=&gt;  “索尼”
“产品”=&gt;  “PlayStation 4”
“created_at”=&gt;  “2015-07-04T00:00:00 + 00:00”
] 
] 
“media”=&gt; 数组:2 [
“video”=&gt;  “https://my.domain.com/421156.mp4"
”images“=&gt;  “https://my.domain.com/tv/thumbs/421156-1.jpg"
] 
”statistics“=&gt; 数组:3 [
“运行”=&gt;  172 
“firstseen_at”=&gt;  “2015-07-04T19:06:41 + 00:00”
“lastseen_at”=&gt;  “2015-07-09T12:04:13 + 00:00”
] 
“广播”=&gt;  array:172 [] 
] 
   
 
 

如何从此响应中获取单个值? 假设我要显示,或者将“manufacturer”的值分配给另一个变量,并在另一个变量中分配运行次数(“runs”)?

对于制造商,我尝试过这样的事情:

  dd($ apiResult [“campaign”]  - &gt; manufacturer); \  n   
 
 

但是显示错误 - 尝试获取非属性对象!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dotn30471 2017-07-10 09:21
    已采纳

    You're mixing object access with array access.

    $apiResult = json_decode($r->getBody(), true);
    dd($apiResult["campaign"][0]["manufacturer"]);
    

    or with objects

    $apiResult = json_decode($r->getBody(), false);
    dd($apiResult->campaign[0]->manufacturer);
    
    已采纳该答案
    打赏 评论
  • duanduan8439 2017-07-10 09:22

    Looks like campaign is an array so you would need to do:

    $apiResult["campaign"][0]->manufacturer
    
    打赏 评论
  • douming4359 2017-07-10 09:33

    If you want to play with object then use following:

    $apiResult = json_decode($r->getBody());
    
    dd($apiResult->campaign[0]->manufacturer);
    

    or want an associative array

    $apiResult = json_decode($r->getBody(), true);
    
    dd($apiResult["campaign"][0]["manufacturer"]);
    

    For more detail read documentation of json_decode

    打赏 评论

相关推荐 更多相似问题