Switched API from openweathermaps need some help ( using newtonsoft.json nuget ) c#

  • 1
  • Problem
  • Updated 3 years ago
  • (Edited)

Hello everyone,

My  previous code using openweather maps : http://hastebin.com/uzebomohon.vala

The problem im having right now is: 

 

I want to switch from openweathermap.org API to wunderground because it allows weather forecast by IP and is said to be more accurate. But seems a bit more complicated to convert and http://json2csharp.com/  isn't cutting it...

can someone please help me out?


Yes im using unity but I (think) I did everything needed for any weather API to work.

If I just know how to convert this to c sharp and properly log everything to console that should wrap my head around it enough to use it for whatever else so any help is greatly appreciated!

Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes

Posted 3 years ago

  • 1
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
Even if having poor .NET skills hoping to be helpful.
You posted the class you're using with openweathermap, but what are the issues you're facing in writing the one to be binded to weatherunderground?
Generating this type of C# class looks pesky but not impossible.

By doing a quick search, an alternative method is available by using JObject.Parse from Newtonsoft.Json.Linq, traversing the JObject path and casting the wanted values as string 
see for more at 
http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_Parse.htm
http://www.newtonsoft.com/json/help/html/QueryJson.htm
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
Thanks for your reply!

I guess I simply assumed my classes generated from openweathermap.org using http://json2csharp.com/  werent going to work because when i tried to convert weatherunderground the same way i got something completely broken and screwy out of it instead
(Edited)
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
You're welcome_

I tried to use that webtool with an addess like
http://api.wunderground.com/api/_API_KEY_HERE_/forecast/q/autoip.json
and from what I can see it can parse, but you may need to manually rework the code for types and for wrapping class (I don't get the match between the raw JSON and the WeatherData in the hastebin code)
(Edited)
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
its 2 am here right now so im going to check this out tomorrow morning! thanks afelicioni :)
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
ok, you're welcome; feel free to post progresses, we're curious about (same time zone for me)
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
will do!
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
Ive had some help with this and this is how its going so far: http://hastebin.com/finufacika.vala  still dicking around on getting the right values out of the autoip json instead, with the models specific to it. instead of using that convert website using Visual Studio 2015's paste as special (json) functionality is very good!
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
just not sure how to get the value of likelyhood to rain or just the fact that it is raining / gonna rain ( or not )
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
well done for developing, the class code looks very straightforward

for detecting a particular condition, you can do a string cross compare with list available at http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary for conditions or icon

since forecast is given as a list, a pseudocode consisting in a loop with a cursor could refer to weatherData.forecast.simpleforecast.forecastday[cursor].conditions in a foreach cursor in weatherData.forecast.simpleforecast.forecastday
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
I'm not sure im following haha, english isnt my Main language but I googled POP and learned that ment probability of precipation and is apparantly the chance of rain? https://www.washingtonpost.com/blogs/capital-weather-gang/wp/2015/04/30/poll-what-does-probability-o... 

Youre saying i can also use the conditions?  Also I know what pseudocode means but what is a cursor? :)



Edit: Do you have any idea what would be a good way to actually use POP to make it rain? 



When I think about it, we only get the probability of todays weather and not the actual weather.

And its going to look very silly when it is raining outside but not in the program or IS raining in the program but not actually outside....

Because the pop value in the code just so happens to be set low enough for it to happen....wondering how to go about that.


Any ideas are greatly appreciated!




Second edit: Also is it normal for the forecast to be changing on almost every single refresh i do?

It seems to be alternating constantly between:

"fcttext":"Cloudy this morning with showers during the afternoon. High 64F. Winds SSW at 10 to 20 mph. Chance of rain 50%.",
"fcttext_metric":"Cloudy with periods of light rain. High 17C. Winds SSW at 15 to 30 km/h. Chance of rain 40%.", "fcttext":"Cloudy this morning with showers during the afternoon. High 64F. Winds SSW at 10 to 20 mph. Chance of rain 50%.",

and
"fcttext":"Cloudy this morning with showers during the afternoon. High 64F. Winds SSW at 10 to 20 mph. Chance of rain 50%.",
"fcttext_metric":"Cloudy with periods of light rain. High 17C. Winds SSW at 15 to 30 km/h. Chance of rain 40%.",
"pop":"50"
(Edited)
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
Extra thoughts: I may have the % chance of rain

but its never certain whether its actually ''raining'' and its going to look really weird to have rain happen in the game when there is actually non going on outside, because thats kind of the point!

Here's the whole script incase that helps: 

http://hastebin.com/erupeqelar.vala (I commented it all out for you so it makes sense hopefully)

What do you think? Is the solution in doing some sort of combination of using these?

From the glossary:

precipm: Precipitation in mm 

precipi: Precipitation in inches

pop:Probability of Precipitation

And create a formula to increase accuracy? or do you think the solution lies more likely in the current condition?

condition:conds ( cant seem to parse this btw) it isnt returning values when I go for string something = weatherData.forecast.simpleforecast.forecastday[0].conditions;

Because im thinking the actual descriptions might actually be more helpful like when its says its going to be cloudy with ocasional light rain if we could convert that to usable values or something?
(Edited)
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
How accurate are Current Condition Phrases  exactly? I think I may have this figured out because parsing conditions is suddenly not a problem.
(Edited)
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
So I got it to work exactly how I want it to now, grabbing the string output from the
conditions array and then creating an enum list containing all the condition phrases and then comparing them to the string output from the conditions array.

I get the current condition string output, it will go through my enum list, and if the current condition enum is equal to the current condition string output then : stuff happens!

However I hope the the API's condition information is accurate enough to know whether its truly currently raining outside or not... 
(Edited)
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
hi, seen a lot of improvements... well done

since I can mainly follow the programming aspects, I hope a Weather Underground representative can be clear about forecast "reliability index" to ensure the best success of your project.

about forecast response model, under simpleforecast you get today forecast and for next 3 days, but under txt_forecast it doubles since it returns splitted day and night forecasts, codeable via a for loop with index starting from 0; anyway if current conditions is needed, conditionsfeature may fit best http://www.wunderground.com/weather/api/d/docs?d=data/conditions
Photo of afelicioni

afelicioni

  • 227 Posts
  • 43 Reply Likes
hi, seen a lot of improvements... well done

since I can mainly follow the programming aspects, I hope a Weather Underground representative can be clear about forecast "reliability index" to ensure the best success of your project.

about forecast response model, under simpleforecast you get today forecast and for next 3 days, but under txt_forecast it doubles since it returns splitted day and night forecasts, codeable via a for loop with index starting from 0; anyway if current conditions is needed, conditionsfeature may fit best http://www.wunderground.com/weather/api/d/docs?d=data/conditions
Photo of ㅇ새 ㅇ 국ㄸ긴

ㅇ새 ㅇ 국ㄸ긴

  • 12 Posts
  • 0 Reply Likes
I thought i was already using the conditions feature?