How can I print a table, converted from json so that I understand how to access different parts.
I have an json from wunderground, where I want to get the weather text report. I use dzVents.
http://api.wunderground.com/api/<API-KE ... HO432.json
This is what I see when I log the domoticz.json in the HTTPResponse:
{["forecast"]={["txt_forecast"]={["date"]="8:33 AM CET", ["forecastday"]={{["fcttext"]="Övervägande molnigt. Högsta 35ºF. Vindar SSÖ på 10 till 15 mph.", ["title"]="Torsdag", ["icon"]="mostlycloudy", ["pop"]="10", ["fcttext_metric"]="Övervägande molnigt. Högsta 1ºC. Vindar SSÖ på 10 till 15 km/tim.", ["icon_url"]="http://icons.wxug.com/i/c/k/mostlycloudy.gif", ["period"]=0}, {["fcttext"]="Molnigt. Lägsta 29ºF. Vindar SÖ på 5 till 10 mph.", ["title"]="Torsdag natt", ["icon"]="nt_cloudy", ["pop"]="20", ["fcttext_metric"]="Molnigt. Lägsta -2ºC. Vindar SÖ på 10 till 15 km/tim.", ["icon_url"]="http://icons.wxug.com/i/c/k/nt_cloudy.gif", ["period"]=1}, {["fcttext"]="Snöbyar under eftermiddagen. Högsta 36ºF. Vindar SÖ på 5 till 10 mph. Sannolikhet snö 40%.", ["title"]="Fredag", ["icon"]="snow", ["pop"]="40", ["fcttext_metric"]="Snöbyar under eftermiddagen. Högsta 2ºC. Vindar SÖ på 10 till 15 km/tim. Sannolikhet snö 40%.", ["icon_url"]="http://icons.wxug.com/i/c/k/snow.gif", ["period"]=2}, {["fcttext"]="Snöbyar tidigt. Lägsta 28ºF. Vindar NÖ och växlande. Sannolikhet snö 40%.", ["title"]="Fredag natt", ["icon"]="nt_chancesnow", ["pop"]="40", ["fcttext_metric"]="Snöbyar tidigt. Lägsta -2ºC. Vindar NÖ och växlande. Sannolikhet snö 50%.", ["icon_url"]="http://icons.wxug.com/i/c/k/nt_chancesnow.gif", ["period"]=3}, {["fcttext"]="Moln under morgonen följt av eftermiddagssol. Högsta 33ºF. Vindar NÖ och växlande.", ["title"]="Lördag", ["icon"]="partlycloudy", ["pop"]="10", ["fcttext_metric"]="Moln under morgonen följt av eftermiddagssol. Högsta 1ºC. Vindar NÖ och växlande.", ["icon_url"]="http://icons.wxug.com/i/c/k/partlycloudy.gif", ["period"]=4}, {["fcttext"]="Halvklart. Köld förväntas. Lägsta 22ºF. Vindar SSV och växlande.", .. and so on.
I would like a script function that could print something like this (accessor=value)
forecast.forecastday.forecastday.fcttext.metric = "Snöbyar ...
so it is easy to understand how to get data from large cumbersome json files.
Secondly, if someone could help me how I can access the parts in bold.
Thanks/Jan
How can I print a table, converted from json
Moderator: leecollings
-
- Posts: 1
- Joined: Monday 16 October 2017 9:18
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
-
- Posts: 247
- Joined: Sunday 29 November 2015 20:46
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 3.9639
- Location: Spain
- Contact:
Re: How can I print a table, converted from json
Copy and paste the response in Notepad++ and select as language JSon (or install the json viewer plugin in chrome) and you will get exactly what you are asking here.
- McMelloW
- Posts: 434
- Joined: Monday 20 November 2017 17:01
- Target OS: Raspberry Pi / ODroid
- Domoticz version: V2024.1
- Location: Harderwijk, NL
- Contact:
Re: How can I print a table, converted from json
Copy and paste to https://jsonlint.com/ and click on Validate JSON
Greetings McMelloW
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: How can I print a table, converted from json
Then why post in the PHP board and not in the dzvents board?
In PHP that would be peace of cake:
Code: Select all
$wu=@json_decode(@file_get_contents('http://api.wunderground.com/api/a123456789b/conditions/q/BX/Beitem.json'),true);
if(isset($wu['current_observation'])){
//Check when we last had a good response
$lastobservation=apcu_fetch('wu-observation');
//Check if the response is a good one or a estimated, if estimated exit.
if(isset($wu['current_observation']['estimated']['estimated']))goto exitwunderground;
//If timestamp of observation is older than our last successful exit
elseif($wu['current_observation']['observation_epoch']<=$lastobservation)goto exitwunderground;
//Store needed values in vars for later use in script.
if(isset($wu['current_observation']['temp_c'])){$wutemp=$wu['current_observation']['temp_c'];if($wutemp>$prevtemp+0.5)$wutemp=$prevtemp+0.5;elseif($wutemp<$prevtemp-0.5)$wutemp=$prevtemp-0.5;}
if(isset($wu['current_observation']['wind_kph']))$wuwind=$wu['current_observation']['wind_kph'];
if(isset($wu['current_observation']['wind_gust_kph']))if($wu['current_observation']['wind_gust_kph']>$wuwind)$wuwind=$wu['current_observation']['wind_gust_kph'];
if(isset($wu['current_observation']['precip_1hr_metric']))$wubuien=$wu['current_observation']['precip_1hr_metric']*35;
if(isset($wu['current_observation']['wind_dir']))apcu_store('winddir',$wu['current_observation']['wind_dir']);
if(isset($wu['current_observation']['icon']))apcu_store('icon',$wu['current_observation']['icon']);
//Store the timestamp of this observation
apcu_store('wu-observation',$wu['current_observation']['observation_epoch']);
}
exitwunderground:
Who is online
Users browsing this forum: No registered users and 1 guest