Convert JSON And XML Object Into Class Using Visual Studio

Introduction

Creating classes based on JSON/XML responses, from APIs are the most usual work for all developers. Normally, if we want to convert a JSON/XML object into class, then we will check tools online for conversion. Hereafter no need for any online tool. Visual Studio can do this work and generate classes from a JSON/XML using just copy-paste. Do you believe this? Yes, there is. In this article, we are going to explore how to convert a JSON/XML object into the classes using Visual Studio.

Convert JSON into Classes

Step 1

Open your project/solution in Visual Studio 2019.

Step 2

Copy the JSON object for which you want to convert as a class. Let consider the below JSON object and i had copied it.

{
  "student": {
    "name": "Test",
    "degree": "IT",
    "subjects": [{
        "name" : "Computer Science",
        "mark": 95
    },
    {
        "name" : "Maths",
        "mark": 98
    }]
  }
}

Step 3

Go to the file where you want to create a class and place the cursor.

Step 4

Go to Edit -> Paste Special -> Paste JSON as Object

Convert JSON into Classes

Step 5

After clicking the "Paste JSON as Object" menu, the above JSON object is converted into class and pasted in the file as below.

public class Rootobject
{
	public Student student { get; set; }
}

public class Student
{
	public string name { get; set; }
	public string degree { get; set; }
	public Subject[] subjects { get; set; }
}

public class Subject
{
	public string name { get; set; }
	public int mark { get; set; }
}

 

Convert JSON into Classes

You can convert any complex JSON object into the class using the above steps.

Convert XML into Classes

Step 1

Open your project/solution in Visual Studio.

Step 2

Copy the XML object for which you want to convert as a class.  Let consider the below XML object and i had copied it.

<?xml version="1.0" encoding="UTF-8" ?>
<student>
    <name>Test</name>
    <degree>IT</degree>
    <subjects>
        <name>Computer Science</name>
        <mark>95</mark>
    </subjects>
    <subjects>
        <name>Maths</name>
        <mark>98</mark>
    </subjects>
</student>

Step 3

Go to the file where you want to create a class and place the cursor.

Step 4

Go to Edit -> Paste Special -> Paste XML as Object

Convert XML into Classes

Step 5

After clicking the "Paste XML as Object" menu, the above XML object is converted into class and pasted in the file as below.

// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class student
{

	private string nameField;
	private string degreeField;
	private studentSubjects[] subjectsField;
	private string[] textField;

	/// <remarks/>
	public string name
	{
		get
		{
			return this.nameField;
		}
		set
		{
			this.nameField = value;
		}
	}

	/// <remarks/>
	public string degree
	{
		get
		{
			return this.degreeField;
		}
		set
		{
			this.degreeField = value;
		}
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlElementAttribute("subjects")]
	public studentSubjects[] subjects
	{
		get
		{
			return this.subjectsField;
		}
		set
		{
			this.subjectsField = value;
		}
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTextAttribute()]
	public string[] Text
	{
		get
		{
			return this.textField;
		}
		set
		{
			this.textField = value;
		}
	}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class studentSubjects
{

	private string nameField;
	private byte markField;
	private string[] textField;

	/// <remarks/>
	public string name
	{
		get
		{
			return this.nameField;
		}
		set
		{
			this.nameField = value;
		}
	}

	/// <remarks/>
	public byte mark
	{
		get
		{
			return this.markField;
		}
		set
		{
			this.markField = value;
		}
	}

	/// <remarks/>
	[System.Xml.Serialization.XmlTextAttribute()]
	public string[] Text
	{
		get
		{
			return this.textField;
		}
		set
		{
			this.textField = value;
		}
	}
}

Summary 

Converting JSON/XML object into classes using copy-paste in Visual Studio is one of the great feature. Most of the developers are not aware of this feature in Visual Studio. Definitely, this feature has been used to reduce the developer's manual work (convert JSON/XML into class). Cool feature right !!!!!


Similar Articles