Convert JSON to C# Class In Visual Studio
I was working on Integration with one of the social media
This social media provider was returning me a big complex JSON object
I wanted to deserialize this object, and if I want to convert the JSON object into C# class then this will take sometime from me,
So I found a way to do this from Visual Studio,
follow me to know how to do it 👇
Sample JSON data:
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
Step 1: Copy the required JSON object
Step 2: Open your project in Visual Studio
Step 3: Go to the class file that you want to paste your data into
Step 4: Go to Menu Bar >> Edit >> Paste Special >> Paste JSON As Classes
Result:
public class Rootobject
{
public Widget widget { get; set; }
}
public class Widget
{
public string debug { get; set; }
public Window window { get; set; }
public Image image { get; set; }
public Text text { get; set; }
}
public class Window
{
public string title { get; set; }
public string name { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Image
{
public string src { get; set; }
public string name { get; set; }
public int hOffset { get; set; }
public int vOffset { get; set; }
public string alignment { get; set; }
}
public class Text
{
public string data { get; set; }
public int size { get; set; }
public string style { get; set; }
public string name { get; set; }
public int hOffset { get; set; }
public int vOffset { get; set; }
public string alignment { get; set; }
public string onMouseUp { get; set; }
}
nice , thanks man :)
ReplyDeleteAppreciated, Thanks for your comment.
Delete