Consuming JSON from a C# Project

I was thinking about an app that I have wanted to write for a while.  Part of the application uses weather data.  Since I have been using weather underground since forever (Go Blue), that seemed like a natural place to start.  I hopped over to wunderground and sure enough, at the bottom of the page is a link for consuming its API. 


Following the Weather API for Developers – JSON link, I signed up for a free developer account.  Within minutes I got my API key and I was ready to start looking for the data I was interested in for my application. I first went to the documentation page, I started with the current conditions for a city example:

I then switched out the state/city criteria with my zip code:

Sure enough, data is getting returned.


Now that I have a source of data, I need a way to consume the data using Visual Studio.  I fired up a Console project just to see how difficult it would be to consume.  I put in a typical webrequest/response:

Uri uri = new Uri(@"");
WebRequest webRequest = WebRequest.Create(uri);
WebResponse response = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
String responseData = streamReader.ReadToEnd();

Sure enough, I am getting the data back:


The next step is to parse this string so I can do something useful with it.  Fortunately, I was coding this solution during a meeting of Cary’s Technology Task Force and I was sitting next to an OData/JSON expert: Ian Cillay .  Ian told me that to parse Json from VS, I needed to get Newtonsoft’s Json.NET framework because MSFT has essentially given up on the parsing and is deferring everything to NewtonSoft.  Once I had that installed via NuGet, I the realized I still needed a class pre-defined to populate from the Json feed.  I fired up a new class with a couple of properties:

public class WeatherData
    public String pressue_mb { get; set; }
    public String pressume_in { get; set; }
    public String pressure_trend { get; set; }
    public String relative_humidity { get; set; }

I then coded up line to use that Json library to put the data from the response string into an instantiated class:

var outObject = JsonConvert.DeserializeObject<WeatherData>(responseData);

However, when I ran it, nothing was getting populated.


Ian told me that my simple class did not reflect the actual Json.  Stumped (and still hating Json), I then stumbled across this site.  Sure enough  it was like SVCUTIL.exe for Json.  I got the classes created for me


and I then could consume the data like this:

var outObject = JsonConvert.DeserializeObject<RootObject>(responseData);

and the output is


Wahoo!  I will use the Visual Studio’s External Tools Feature to generate classes for me on the fly:


17 Responses to Consuming JSON from a C# Project

  1. Brandon says:

    Works great….

  2. John Sully says:

    Thank you, thank you, thank you. I really didn’t want to write all of these classes by hand!

  3. Ivan Borges says:

    Thank you so much for sharing!
    Worked perfectly.

  4. Timothy says:

    This wouldn’t require you to make a proxy for every us city would it?
    Sorry if I am asking a stupid question. I am trying to use wunderground to get its alerts for my weather app, which allows anyone in the us to get weather related alerts and basically get alerted about them so if there is a tornado spotted they can go and run and take cover before it affects them.

  5. Thank you so much for sharing!
    Worked perfectly.

  6. very well bro (y) worked perfect

  7. Robert says:

    You are a lifesaver!!!

  8. asildagli says:

    Thanke You..!!! ALLAH RAZI OLSUN

  9. oooooooooooooooooohhhhhhhhhhhhhh goodnesssssssssssss …..thank you man!!!!

  10. Doug Clavell says:

    No Bueno…

    string URI = “”;
    WebRequest weatherRequest = WebRequest.Create(URI);
    WebResponse weatherResponse = weatherRequest.GetResponse();
    StreamReader weatherStream = new StreamReader(weatherResponse.GetResponseStream());
    string weatherString = weatherStream.ReadToEnd();
    ProgLog.Log(weatherString, PROCNAME, MODNAME);

    //WeatherContract weather = JsonConvert.DeserializeObject(weatherString);
    CurrentObservation currentObservations = JsonConvert.DeserializeObject(weatherString);

    All properties of currentObservations are BLANK or 0
    What gives, Professor?
    – Doug

  11. Doug Clavell says:

    Not seeing where you actually put the data from JSON into data classes.
    When I attempt to parse into classes from deserilization object I need a data contract

  12. Doug Clavell says:

    No data contract but MUST “cast” desrialize result to RootObject like this…
    var currentObservations = JsonConvert.DeserializeObject(weatherString);

  13. Doug Clavell says:

    Previous code sample should have read…
    var currentObservations = JsonConvert.DeserializeObject(weatherString);

    My copy paste skills are lacking. 😦

  14. Doug Clavell says:


    “var currentObservations = JsonConvert.DeserializeObject(weatherString);”

  15. Doug Clavell says:

    Why is site leaving out my cast to RootObject ???????

  16. Doug Clavell says:

    Got it working.

    Very cool and very nice write up. Very clear – except for boneheads like me!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: