0

我在我的网站上使用 zillow api

{
request: {
address: "13546 36th Ave NE",
citystatezip: "Seattle, WA"
},
message: {
text: "Request successfully processed",
code: "0"
},
response: {
results: {
result: {
zpid: "48897608",
links: {
homedetails: "http://www.zillow.com/homedetails/13546-36th-Ave-NE-Seattle-WA-98125/48897608_zpid/",
graphsanddata: "http://www.zillow.com/homedetails/13546-36th-Ave-NE-Seattle-WA-98125/48897608_zpid/#charts-and-data",
mapthishome: "http://www.zillow.com/homes/48897608_zpid/",
comparables: "http://www.zillow.com/homes/comps/48897608_zpid/"
},
address: {
street: "13546 36th Ave NE",
zipcode: "98125",
city: "Seattle",
state: "WA",
latitude: "47.727631",
longitude: "-122.289193"
},
zestimate: {
amount: "393735",
last-updated: "03/12/2017",
oneWeekChange: {
@attributes: {
deprecated: "true"
}
},
valueChange: "24606",
valuationRange: {
low: "374048",
high: "413422"
},
percentile: "0"
},
localRealEstate: {
region: {
@attributes: {
name: "Cedar Park",
id: "271831",
type: "neighborhood"
},
zindexValue: "547,700",
links: {
overview: "http://www.zillow.com/local-info/WA-Seattle/Cedar-Park/r_271831/",
forSaleByOwner: "http://www.zillow.com/cedar-park-seattle-wa/fsbo/",
forSale: "http://www.zillow.com/cedar-park-seattle-wa/"
}
}
}
}
}
}
}

以上是我从 Zillow Api 得到的响应,我从响应中获取了值,如下所示:

zpid => 48897608
links:
homedetails => http://www.zillow.com/homedetails/13546-36th-Ave-NE-Seattle-WA-98125/48897608_zpid/
graphsanddata => http://www.zillow.com/homedetails/13546-36th-Ave-NE-Seattle-WA-98125/48897608_zpid/#charts-and-data
mapthishome => http://www.zillow.com/homes/48897608_zpid/
comparables => http://www.zillow.com/homes/comps/48897608_zpid/
address:
street => 13546 36th Ave NE
zipcode => 98125
city => Seattle
state => WA
latitude => 47.727631
longitude => -122.289193
zestimate:
amount => 393735
last-updated => 03/12/2017

我使用下面的代码获得了这个:

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

现在我的问题是如何获得如下格式的单个值?

<li>zipid:48897608</li>
<li>low:374048</li>

<li>last-updated:03/12/2017</li>
4

2 回答 2

0

我认为答案在您的问题中,只需修改您的if条件:

echo '<ul>';
foreach ($jsonIterator as $key => $val) {
    if(!is_array($val)) {
        echo '<li>'.$key.':'.$val.'</li>';
    } 
}
echo '</ul>';
于 2017-03-14T10:46:33.070 回答
0

这是因为列表标签在页面上被处理为html标签。换句话说,它不是作为纯文本处理的。在这种情况下,我们应该将 '<' 和 '>' 替换为 '<' 和 '>'。

这是代码行。

foreach ($arr as $key=>$val) {
    echo "&ltli&gt{$key}:{$val}&lt/li&gt";
    echo '<br>';
}

希望它可以帮助你。

谢谢!

于 2017-03-14T10:40:54.707 回答