I have been looking for some time to find a solution to this issue. I'm trying to add the Weather Unlocked local weather API (https://developer.weatherunlocked.com/documentation/localweather) to my site. However I am only able to return all current values.
I want to be able to take out specific items, such as temperature, humidity or latitude/longitude without everything else coming up too.
CODE
const string WEBSERVICE_URL = "http://api.weatherunlocked.com/api/current/51.50,-0.12?app_id=42fd0793&app_key=cd2365f533caad77dc2d874aabc1625b";
try
{
var webRequest = WebRequest.Create(WEBSERVICE_URL);
if (webRequest != null)
{
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
webRequest.Headers["X-API-Key"] = "cd2365f533caad77dc2d874aabc1625b";
//Get the response
WebResponse wr = webRequest.GetResponseAsync().Result;
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream);
string content = reader.ReadToEnd();
lblContent.Text = content;
}
}
catch (Exception ex)
{
lblContent.Text = ex.ToString();
}
Result
{
"lat":51.5,
"lon":0.05,
"alt_m":5.0,
"alt_ft":16.4,
"wx_desc":"Clear skies",
"wx_code":0,
"wx_icon":"Clear.gif",
"temp_c":7.0,
"temp_f":44.6,
"feelslike_c":2.6,
"feelslike_f":36.68,
"humid_pct":70.0,
"windspd_mph":19.26,
"windspd_kmh":31.0,
"windspd_kts":16.74,
"windspd_ms":8.61,
"winddir_deg":250.0,
"winddir_compass":"WSW",
"cloudtotal_pct":25.0,
"vis_km":10.0,
"vis_mi":6.21,
"vis_desc":null,
"slp_mb":1002.0,
"slp_in":29.67,
"dewpoint_c":1.93,
"dewpoint_f":35.47
}
Clearly this is messy and not everything is necessary.
Thanks in advance!
EDIT I tried deserializing and I'm getting the following error:
System.NullReferenceException: Object reference not set to an instance of an object
I get this when I write the foreach loop in my deserialization. My guess is that it's calling an object of type "Weather" (Similar to how object facebookFriend was of type Facebook in the given examples) and nothing is being stored in the weather object. Maybe i'm doing something wrong in the call from the API and I should be storing it in my Weather class?