首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Groupon API获取JSON数据

如何从Groupon API获取JSON数据
EN

Stack Overflow用户
提问于 2017-04-30 05:25:44
回答 2查看 719关注 0票数 0

我正在努力从下面的Groupon API中获取"merchant“-> "id”,而我在返回discountPercent时没有任何问题。

代码语言:javascript
复制
<?php

$url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
$content = file_get_contents($url);
$json = json_decode($content, true);

foreach($json['deals'] as $results) {

$discountPercent = $results['options'][0]['discountPercent'];
$merchantId = $results['merchant'][0]->id;

    echo $discountPercent.'<br>'; 
    echo $merchantId;  

}   
?>

如果有人能告诉我正确的方向的话。

非常感谢,

EN

回答 2

Stack Overflow用户

发布于 2017-05-01 02:22:29

好的,这是答案:

代码语言:javascript
复制
$merchantId = $results['merchant']['id'];
票数 0
EN

Stack Overflow用户

发布于 2017-12-17 05:27:19

代码语言:javascript
复制
>>>This may be the Reason.....

>>>Explanation for::  $results['options'][0]['discountPercent']  
In $json array you can see
[options] => Array  
 (  
  [0] => Array  
   (  
    .........  
    [discountPercent] => 54    
     .........     
    )  
 )   
Here, the hierechy is  
option->0(key)->discountPercent  
that's why you need to use index '0'; 

 >>>Explanation for:::  $results['merchant'][0]->id  
in json array you can see  
[merchant] => Array  
 (  
  [id] => global-cuisine-restaurant  
  [uuid] => 183dd76b-a1a6-40cf-93c7-33e00f379451  
  [name] => Global Cuisine Restaurant  
  [websiteUrl] => http://www.globalcuisine.ie/  
  [twitterUrl] =>   
  [facebookUrl] =>   
  [ratings] =>   
 ) 
Here the hirerchy is:::  
merchant->id (notice '0' is not present as any subarray index)   
that's why should use  
 $merchantId = $results['merchant']['id']; 

 finally use can use code::  

        <?php 
            $url = 'https://partner-int-api.groupon.com/deals.json?country_code=IE&tsToken=IE_AFF_0_200012_212556_0&division_id=dublin&offset=0&limit=10';
            $content = file_get_contents($url);
            $json = json_decode($content, true);

            foreach($json['deals'] as $key=>$results) {
                $discountPercent = $results['options'][0]['discountPercent'];  
                $discountPercent[] = $results['options'][0]['discountPercent'];//to get all in one;   
                $merchantId = $results['merchant']['id'];  
                $merchantId[] = $results['merchant']['id'];//to get all in one  
                    echo $discountPercent.'<br>'; 
                    echo $merchantId;  
            }
        ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43700649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档