JSON is a very popular lightweight data-interchange format. Visual Studio makes it very easy to generate class / classes out of JSON. Say, we have the following JSON available:
{ "players": { "player": [ { "playerId": 1, "playerName": "Crosby", "gamesPlayed": 100 }, { "playerId": 2, "playerName": "Niedermayer", "gamesPlayed": 300 } ] } }
To generate C# classes out of the above JSON, copy the JSON to clipboard, in Visual Studio click Edit > Paste Special > Paste JSON as Classes.
It will generate following classes:
public class Rootobject { public Players players { get; set; } } public class Players { public Player[] player { get; set; } } public class Player { public int playerId { get; set; } public string playerName { get; set; } public int gamesPlayed { get; set; } }
Now you can fine-tune some of the code, e.g. change Player[] to List<Player> etc.
This paste special option works well in Visual Studio 2015. In case if you have any older version of Visual Studio then you might want to
add NewtonSoft JSON NuGet package to your project to make it work: