Skip to content
1 min read

Visual Studio JSON to Classes

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.

Share
  
DON'T MISS!

Expert Insights from the Aerie Team

Drop us a line

Our team will be happy to answer your questions.

ADDRESS: 110 West Canal Street, Suite 201
Winooski, VT 05404

PHONE
: (813) 358-4656
HOURS: 8:00 am - 5:00 pm EST Monday to Friday
EMAIL: chanson@aerieconsulting.com