Visual Studio tip – Paste JSON As Classes

A nice ability was added to Visual Studio 2012 (With ASP.NET and Web Tools): pasting a JSON string on Visual Studio as classes.

This shortcut could save a lot of time, having a certain JSON, that needs to be parsed on the server, built automatically by Visual Studio as classes the JSON contains.

In order to do so, I found on the website mockJSON a pretty long JSON:

large json

and copied it to my clipboard.

On Visual Studio, I created a new class, but erase the class that was created, as the VS will create the classes according to the JSON.

On VS’s top bar, under EDIT->Paste Special, choose the option of Paste JSON As Classes:

parse Json

and, et voilà, three new classes:

classes from json

 

Some things to take in consideration:

  • If, like the top level here, a property doesn’t have a name, the class gets an arbitrary name, like Rootobject here.
  • The JavaScript convention uses lower case as the property’s first letter, unlike the C# convention.
    Thus, the property here is called fathers, and not Father.

This trick should work for XML as well.

 

I’ve seen this cool and useful tip in a lecture given by David Catuhe & Jon Galloway about ASP.NET and AngularJS.

Worth watching or listening. Catch is here.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*