Visual Studio JSON to Classes

Posted by Beau Cameron on 04.23.15
Beau Cameron
Find me on:

Last post I talked a little about how I enjoy using REST and JSON instead of the Client Object Model. When developing I feel much more comfortable working with REST APIs and recently had a need to consume JSON on the server using C# and .net. The normal process would be to retrieve a JSON object, parse it into C# classes that you've created to consume on the server and then do something with the data.

If you are working with large JSON objects, this can be a time consuming task. Lets say you had a JSON object that would result in say 40 C# classes, you'd be pretty bummed to take on the tedious task of creating each one by hand. Well luckily, Visual Studio 2012 and higher has given us the ability to automate this process and allow us to continue working on the important things. 

Let's take this sample JSON object below (pulled from json.org).jsonSample

 

If I wanted to then take this JSON structure and convert it into C# classes, I would COPY the JSON to my clipboard, open up Visual Studio 2012+, click Edit, Paste Special , Paste JSON as Classes.


pastSpectial

and VOILA! Visual Studio will turn our JSON object in the C# equivilent in classes like below.

Class

When the time comes to actually consume this data, I would just need to serialize the JSON object into a new RootObject()... and from there on I am free to work with C# objects.

Topics: Visual Studio

Subscribe Here!

Recent Posts

Posts by Tag

See all