Quick Tip: Create classes from JSON and XML C# Visual Studio 2019

albertbennett

Albert Bennett

Posted on September 22, 2021

Quick Tip: Create classes from JSON and XML C# Visual Studio 2019

If you learned something new feel free to connect with me on linkedin or follow me on dev.to :)


I have a quick one for you today.
I recently seen this, and wanted to share how it can be done for those who don't know about it.
It's a great timesaver especially if you need\ want to parse JSON responses or files without having to use JObjects or dynamic objects.


Step 1:
To start you'll need to make sure that in Visual Studios you have the 'Web Development Tools plus .NET Core 2.1' package installed in Visual Studios. You can do this by opening up the Visual Studio Installer then going to: Modify > Individual components and then select the package 'Web Development Tools plus .NET Core 2.1' and click install.
image

Step 2:
Check to see if you have the 'paste special' option under edit in the toolbar. If not Restart your machine. It should show up in Visual Studios afterward.
image
To test this feature out I used the following JSON code. I found the sample code on https://json.org/example.html:

{
    "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"
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

And here is the result:

    public class Rootobject
    {
        public Glossary glossary { get; set; }
    }

    public class Glossary
    {
        public string title { get; set; }
        public Glossdiv GlossDiv { get; set; }
    }

    public class Glossdiv
    {
        public string title { get; set; }
        public Glosslist GlossList { get; set; }
    }

    public class Glosslist
    {
        public Glossentry GlossEntry { get; set; }
    }

    public class Glossentry
    {
        public string ID { get; set; }
        public string SortAs { get; set; }
        public string GlossTerm { get; set; }
        public string Acronym { get; set; }
        public string Abbrev { get; set; }
        public Glossdef GlossDef { get; set; }
        public string GlossSee { get; set; }
    }

    public class Glossdef
    {
        public string para { get; set; }
        public string[] GlossSeeAlso { get; set; }
    }
Enter fullscreen mode Exit fullscreen mode

It is a really cool result and a massive timesaver as the time it takes to convert the JSON to C# is milliseconds instead of well however long it takes to write the C# conversion yourself.


Notes:
So there are a few things to make note with this feature.

  1. Depending on your use case it is probably better to to use the 'paste special' command in a blank .cs file. How it works is that it pastes the converted code exactly where your cursor is. So, it can be a bit annoying of you did it inside of an established class for example.
  2. I couldn't find this edit option in every project. I've tried to find it on a .net 5.0 Windows Forms project and the option wasn't there. However, you should be able to see the option in most web based applications. I've seen this feature and tested it out on Function Apps, Chat Bot and ASP.Net core projects.

Thanks for reading my post, I'll see you in the next one!

💖 💪 🙅 🚩
albertbennett
Albert Bennett

Posted on September 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related