Thursday, May 10, 2018

From JSON to C#


The problem is common. As a developer you are handed JSON (mostly likely from a RESTful web service). From said JSON you need to generated the corresponding C# classes.

Solution 1: Web Essentials

The approach with the tightest Visual Studio integration is installing the Web Essentials extension found at http://vswebessentials.com. After installing the extension create a C# file into which C# code will be generate. Below in example of a source file just waiting to be filled with C# generated from JSON:


Notice in the screenshot above the cursor is left where class will be generated.

Select the JSON text and copy it to the clipboard (CTRL-C). Once this open Visual Studio's Edit menu and select Past Special | Paste JSON As Classes:


An example of the code Paste Special |mJSON as Classes can generate is as follows:



Solution 2: json2csharp.com

The web site http://json2csharp.com provides the same service as the Web Essentials, Visual Studio Extension, Paste JSON As Classes. This site is simple to use. Paste in the JSON and click on the Generate button. An example of this is as follows:



No comments :

Post a Comment