Consuming JSON web APIs with Visual Studio

, posted: 16-Dec-2011 09:59

Most web APIs (including the recently introduced Geekzone API) will happily return a JSON result. Using a little serializing and de-serializing magic from JSON.NET it's now a trivial task to consume such services in a nice object oriented way. In this blog post I will show how to do it with Visual Studio.

Take for example the ForumsList method of the Geekzone API, which will (oddly enough) return the list of available forums, and their associated details such as a description, topic and post counts, etc.

Lets mock up how we would consume this service call. First, we'll create a local class structure in our project - into which we are able to map the results we expect back from our API call. In this case we'll create a 'Forum' class. We will be serializing our JSON results into this class, giving us a nice, friendly, strongly typed interface to work against (we like those).


 
Next we'll add a reference to the JSON.NET project (in this case I'm using Nuget to do so).



Then, using the WebClient class of the .NET Framework, we initiate an asynchronous HTTP GET request to the API URL, which will result in a JSON response string being returned.



On first glace the returned JSON string resembles a jumble of text, but it's actually a structured way of representing hierarchical data (similar to XML). Read a little more background on JSON (Javascript object notation) over at JSON.org if you wish.



In this case, what we have returned is a JSON string representing a collection of forum results. You'll also see within the structure that there are fields which match up perfectly with our 'Forum' class we created earlier (what a co-incidence). This convention means we'll easily be able to auto-magically map these strings into our strongly typed class structure.

Now using the JSON.NET converter class to serialize our string of textual information into a class structure we are left with a 1 to 1 mapped object which retains all hierarchy and structure ready to be used.



So there we go, a couple of small method calls and a little help from our friend JSON.NET and we're consuming a REST API and mapping results to nice friendly objects.

PS. Lookout for additions to the Geekzone API coming soon (including better documentation!).

Download a trial of Visual Studio.

About the author

Phil Gale is a Software Architect and Graphic Designer with over a decade of hands on experience in the web and software industry. He is a director at Red Jungle where they make fantastic software with exceptional user experiences. You can catch him twitter at @redjungle and on Geekzone as well.

Other related posts:
Secure Development Tips and Techniques for use in Asp.net websites and web applications
Web.config transforms






Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and shown in this blog post.

Your name:

Your e-mail:

Your webpage:

About the Visual Studio blog

In the years since the hugely successful release of Visual Studio 2005, Microsoft has used developer feedback from all over the world to introduce new features in later releases.

This sponsored blog will bring Visual Studio tips and tricks from well known developers in the New Zealand tech community directly to you.

Every second day during November and December 2011 you will find something new here. Make sure you bookmark this blog or subscribe to its RSS feed.    

Join us on Twitter

Find us on Facebook

Tags

Blog...
Database...
Techniques...
Testing...
Tools...
User Interface...
Windows Phone...


Other blog posts

Winners of Windows Phone compe...
Customise your Visual Studio 2...
Secure Development Tips and Te...
NuGet Package Manager extensio...
Competition: be in to win one ...
Recommended documentation add-...
Test driven development in Vis...
Web.config transforms...
Getting Started with Visual St...
Welcome to the Visual Studio b...


Recent comments

kiwiandy on Winners of Windows Phone competition announced, mo: mm the first one I try... ASB.. isnt there!?...

chiefie on Winners of Windows Phone competition announced, mo: Hmm the ASB Bank app is MIA....

Daniel Ballinger on Recommended documentation add-on for Visual Studio: Personally I don't find much value from a tool like GhostDoc if the generated do...



Disclaimer

The Visual Studio blog is sponsored by Microsoft NZ. The blog posts are the authors' genuine accounts of their experiences with Visual Studio, Windows Phone SDK and Windows Azure Platform and are not influenced or filtered by Microsoft New Zealand in any way.