Generate Class From JSON or XML in Visual Studio

Introduction

This article is about a cool feature in Visual Studio that helps save the effort of a developer to generate class a structure from a JSON or XML string.

There is cases application written using the .NET Framework written by a developer that receives a JSON or XML string from a web service or from another third party application. After receiving JSON or XML string data there is a need for further processing of the received data that requires deserialization of the received JSON or XML in a .Net object. And for that a .Net class structure must be created to match the JSON or XML string data so that the JSON or XML string can be deserialized correctly.

Problem Statement

For example, a program receives the following JSON string by calling a web service or third-party program.

JSON

  1. {"employees":[  
  2.    {"firstName":"John""lastName":"Doe"},  
  3.    {"firstName":"Anna""lastName":"Smith"},  
  4.    {"firstName":"Peter""lastName":"Jones"}  
  5. ]}  
XML
  1. <employees>  
  2.     <employee>  
  3.         <firstName>John</firstName>  
  4.         <lastName>Doe</lastName>  
  5.     </employee>  
  6.     <employee>  
  7.         <firstName>Anna</firstName>  
  8.         <lastName>Smith</lastName>  
  9.     </employee>  
  10.     <employee>  
  11.         <firstName>Peter</firstName>  
  12.         <lastName>Jones</lastName>  
  13.     </employee>  
  14. </employees>  
And now there is the need for deserializing the received JSON or XML string to a .Net object so further processing can be done based on the received data or on the received data.

Approach 1: Do it manually

In this approach the developer must understand JSON or XML so that he/she can understand the received JSON or XML string and its structure. Once getting the details of JSON or XML and the received JSON or XML string, the developer must convert the JSON or XML string into a meaningful class structure so desterilization can be done without a problem.

So the problem with this approach is so much work must be done by the developer, like: 
  1. Requires knowledge of JSON or XML.

  2. Requires an understanding of the JSON or XML string sent by the exteranl program.

  3. Creates class strucutre for the JSON or XML structure that is a trial and error approch because usually the created structure doesn't work in the first go.

  4. If a JSON or XML string has a complex structure then its difficult and requires time to create the class structure to deserialize the JSON or XML string.

Approach 2: Automated using Visual Studio

This approach uses Visual Studio to generate a class just by copying and pasting the JSON or XML string.

The following is the procedure to generate the class:

  1. Copy JSON or XML string

    JSON

    JSON


    XML

    XML

  2. Go to Edit > Paste Sepcial > Paste JSON As Classes or Paste XML As Classes.

    past spacial

  3. Visual Studio generates a class structure for the developer as in the following:

    The following is an example of the class structure created by copying and pasting a JSON string.
    1. public class EmployeeList {  
    2.     public Employee[] employees {  
    3.         get;  
    4.         set;  
    5.     }  
    6. }  
    7.   
    8. public class Employee {  
    9.     public string firstName {  
    10.         get;  
    11.         set;  
    12.     }  
    13.     public string lastName {  
    14.         get;  
    15.         set;  
    16.     }  


    17.  

Below is example of class structure created by copy and pasting XML string

  1. /// <remarks/>    
  2. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    
  3. [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]    
  4. public partial class employees {    
  5.     
  6.     privateemployeesEmployee[] employeeField;    
  7.     
  8.     /// <remarks/>    
  9.     [System.Xml.Serialization.XmlElementAttribute("employee")]    
  10.     publicemployeesEmployee[] employee {    
  11.         get {    
  12.             returnthis.employeeField;    
  13.         }    
  14.         set {    
  15.             this.employeeField = value;    
  16.         }    
  17.     }    
  18. }    
  19.     
  20. /// <remarks/>    
  21. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    
  22. public partial class employeesEmployee {    
  23.     
  24.     private string firstNameField;    
  25.     
  26.     private string lastNameField;    
  27.     
  28.     /// <remarks/>    
  29.     public string firstName {    
  30.         get {    
  31.             returnthis.firstNameField;    
  32.         }    
  33.         set {    
  34.             this.firstNameField = value;    
  35.         }    
  36.     }    
  37.     
  38.     /// <remarks/>    
  39.     public string lastName {    
  40.         get {    
  41.             returnthis.lastNameField;    
  42.         }    
  43.         set {    
  44.             this.lastNameField = value;    
  45.         }    
  46.     }    

 
As you see in the preceding code it will generate the correct attributes for the XML in the class so the deserialization is done without problems.

The advantages of this solution are:
  1. No need to understand JSON or XML
  2. No effort require for creating the class
Summary

The Visual Studio feature for creating a class based on a string is really helpful for developers because an understanding of the JSON or XML string and the creation of the class structure requires effort and is sometimes very frustrating.

Up Next
    Ebook Download
    View all
    Learn
    View all