我一直在尝试阅读以下网址的json响应
https://partner-int-api.groupon.com/deals.json?country_code=IN&tsToken=XXXXXXXXXXXXXXXXXX&CID=XXXXXXXXXXXXXXXXXX&nlp&utm_source=GPN&utm_medium=afl&utm_campaign=200321
或click here可以访问它。
我想得到
"price": {
"amount": 49900,
"formattedAmount": "Rs.499.00",
"currencyCode": "INR"
}
如何阅读?
foreach($datas as $d)
{
$list.= "<div class=frew>";
$list.= "<a href='".$d->dealUrl."'>".$d->announcementTitle."</a>";
$list.= "<div>";
$list.= "<div style='float:left;width:90%'><img src='".$d->mediumImageUrl."' />";
$list.= "<div style='padding-left:2px;float:left'>".$d->title."
<a class=css-button href='".$d->dealUrl."'>Click Here</a> </br>
<strong>Popularity: <a href='".$d->dealUrl."'> $d->soldQuantity </a></strong></br>
</div></div>";
$list.= "</div>";
$list.= "</div>";
}
答案 0 :(得分:1)
由于您知道返回json输出的URL,因此您需要主要两个PHP函数:
因此,您可以执行以下操作:
<?php
$q = "https://partner-int-api.groupon.com/deals.json?country_code=IN&tsToken=XXXXXXXXXXXXXXXXXX&CID=XXXXXXXXXXXXXXXXXX&nlp&utm_source=GPN&utm_medium=afl&utm_campaign=200321";
function readQuery($q){
$data = file_get_contents($q);
return json_decode($data);
}
echo "<pre>";
$res = readQuery($q);
$i = 0;
foreach ($res->deals as $deal){
foreach ($deal->options as $option){
$prices[$i]['amount'] = $option->price->amount;
$prices[$i]['formattedAmount'] = $option->price->formattedAmount;
$prices[$i]['currencyCode'] = $option->price->currencyCode;
}
$i++;
}
//var_dump($res->deals[1]->options[1]->price);
print_r($prices);
上述解决方案将返回一个数组,其中包含JSON资源中的所有价格。
答案 1 :(得分:0)
假设$data
已经包含您链接到的JSON文件中的内容:
$json = json_decode($data);
foreach ($json->deals as $deal) {
$list.= "<div class=frew>";
$list.= "<a href='".$deal->dealUrl."'>".$deal->announcementTitle."</a>";
$list.= "<div>";
$list.= "<div style='float:left;width:90%'><img src='".$deal->mediumImageUrl."' />";
$list.= "<div style='padding-left:2px;float:left'>".$deal->title."
<a class=css-button href='".$deal->dealUrl."'>Click Here</a> </br>
<strong>Popularity: <a href='".$deal->dealUrl."'> $d->soldQuantity </a></strong></br>
</div></div>";
$list.= "</div>";
$list.= "</div>";
}
然后您可以回显$ list以查看该项目。