I have used a YQL query to return a JSON object from Yahoo Weather. However i'm not sure how to markup this object.
The returning JSON object is:
{
"query":{
"count":3,
"created":"2017-12-01T20:28:57Z",
"lang":"en-GB",
"results":{
"channel":[
{
"item":{
"forecast":{
"code":"28",
"date":"01 Dec 2017",
"day":"Fri",
"high":"3",
"low":"0",
"text":"Mostly Cloudy"
}
}
},
{
"item":{
"forecast":{
"code":"26",
"date":"02 Dec 2017",
"day":"Sat",
"high":"7",
"low":"1",
"text":"Cloudy"
}
}
},
{
"item":{
"forecast":{
"code":"26",
"date":"03 Dec 2017",
"day":"Sun",
"high":"8",
"low":"6",
"text":"Cloudy"
}
}
}
]
}
}
}
I can see that the hierarchy is: query > results > channel[item > forecast > WEATHER DATA]
Any help or pointers to webpages / documentation is appreciated. I am also happy to provide any further information if required.
Thanks
You can use json_decode
to easily parse JSON:
<?php
$json = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20title%2C%20units.temperature%2C%20item.forecast%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places%20where%20text%3D%22Stoke%20On%20Trent%2C%20United%20Kindgdom%22)%20and%20u%20%3D%20%22C%22%20limit%203%20%7C%20sort(field%3D%22item.forecast.date%22%2C%20descending%3D%22false%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
$data = json_decode($json);
echo $data->query->count; // Will echo out the value of count, 3 in this case