简体   繁体   中英

json response handling from url using PHP

I am using zillow api on my website

{
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/"
}
}
}
}
}
}
}

Above is the response that i am getting from Zillow Api, I have fetched the values from the response, Something like below:

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

I obtained this using code below:

$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";
    }
}

Now my question is how can get single values like in format below?

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

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

It's because the list tag is processed as html tag on the page. In other words, it's not processed as plain text. In this case, we should replace '<' and '>' with '&lt' and '&gt'.

Here is the code line.

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

Hope it helps you.

Thank you!

I think answer is in your question, just modify your if condition:

echo '<ul>';
foreach ($jsonIterator as $key => $val) {
    if(!is_array($val)) {
        echo '<li>'.$key.':'.$val.'</li>';
    } 
}
echo '</ul>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM