Web services have become the modern way of data exchange.  For .net developers visual studio offers an easy and straightforwardway creating c# classes from a json response object. How is this done? Copy a well formed json response object and then using Visual studio 2012 or later go to edit ->>Paste Special ->>Paste Json as classes as shown in the image below.

 

From the concealed code below, I have a list of type Mood. I will be attempting to see if I can recreate the class from the returned json response. The json response is given below.

 [{“name”:”happy”,”color”:”green”},{“name”:”sad”,”color”:”red”},{“name”:”confused”,”color”:”yellow”}]

On using the procedure given above, corresponding class generated is as shown below

This is pretty much sufficient for our needs as we wont have to do all the typing. And this may not seem like much but consider what happens when one wants to map twitter json response to class. Tweet entities contain so many properties that its might as well render one a typist as opposed to a developer.

 

1 thought on “Create c# entities from json

Leave a Reply

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