c# example

Tagged:

Currently contains 3 posts

Avatar

June 20, 2016 12:42

issharp1

Can someone just give sample c# code to retrieve the forecast using the api? I’ve tried to do it so many different ways but I keep getting errors in my xml document so I really don’t know how to continue. I don’t care if the data comes in as xml or json; I can create the classes, etc, for both. I just need a concrete example of how to retrieve the data and parse it to an object. Thank you so much.

Avatar

June 28, 2016 13:13

issharp1

In case anyone else is having this issue, I’ll post my solution that I eventually (after many hours) came up with. It would’ve been nice for something like this to be located in the code samples for the local weather section, but anyway, here is a method now.

string url = “http://api.weatherunlocked.com/api/forecast/us.” + zip.ToString() + “?app_id={id}&app_key={key}”; WebClient ClientGet = new WebClient(); ClientGet.Headers[“Accept”] = “application/json”; string data = ClientGet.DownloadString(url); var serializer = new DataContractJsonSerializer(typeof(RootObj)); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(data)); var result = (RootObj)serializer.ReadObject(stream); return result;

Where RootObj is the overall type returned by the json call. You can generate classes based on the string that results when you do the ClientGet.DownloadString(url) call. Just add a breakpoint right after that line when running, and then display the string so that you can copy and paste all of the json, and copy it to json2csharp.com and it’ll make your classes for you.

Avatar

June 30, 2016 10:25

John Armstrong

Hi,

We’ve got a sample on the trigger api here – https://developer.weatherunlocked.com/Documentation/WeatherTrigger/CodeSamples

Its broadly the same approach. We will get the samples updated for forecasts – thanks for replying with a sample – very good of you.

cheers

Reply to thread
  1. If you're human, leave this field empty.