Creating Classes from JSON Data in VS2012

If you are using Linq-to-SQL or just about any communication with classes you will eventually need to generate classes that will consume or build JSON responses. The process of building a class that mirrors your JSON data is not difficult to create but are often tedious and time consuming. (I am always looking for ways to cut development time) Microsoft provides us a way to build these classes from within Visual Studio 2012 in a fraction that it takes to build it manually. For example let’s take the JSON sample from here.

{
"glossary": {
   "title": "example glossary",
      "GlossDiv": {
         "title": "S",
	  "GlossList": {
              "GlossEntry": {
                  "ID": "SGML",
	   	   "SortAs": "SGML",
		   "GlossTerm": "Standard Generalized Markup Language",
		   "Acronym": "SGML",
		   "Abbrev": "ISO 8879:1986",
		   "GlossDef": {
                     "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ["GML", "XML"]
                    },
			"GlossSee": "markup"
                }
            }
        }
    }
}

Goto Visual Studio Launch VS2012->Edit->Past Special

Figure1

You get something like this


Public Class Rootobject
    Public Property glossary As Glossary
End Class

Public Class Glossary
    Public Property title As String
    Public Property GlossDiv As Glossdiv
End Class

Public Class Glossdiv
    Public Property title As String
    Public Property GlossList As Glosslist
End Class

Public Class Glosslist
    Public Property GlossEntry As Glossentry
End Class

Public Class Glossentry
    Public Property ID As String
    Public Property SortAs As String
    Public Property GlossTerm As String
    Public Property Acronym As String
    Public Property Abbrev As String
    Public Property GlossDef As Glossdef
    Public Property GlossSee As String
End Class

Public Class Glossdef
    Public Property para As String
    Public Property GlossSeeAlso() As String
End Class

This took about 30 seconds compared to the 25 minutes it would have taken to build the class manually…

Note: Another option is that you can do it online here.

Looking for quality BoonEx Dolphin Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under ASP .NET 2, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

Leave a Reply

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


7 + = 12

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>