JSON is becoming increasingly more popular, it seems like it might be edging XML out as a method for transporting data.
It happens more and more that I need to be able to create a set of classes to represent a JSON file, so that I can work with it in .NET.
In other words, I have this JSON:
{
"id": "0001",
"type": "donut",
"name": "Cake",
"image":
{
"url": "images/0001.jpg",
"width": 200,
"height": 200
},
"thumbnail":
{
"url": "images/thumbnails/0001.jpg",
"width": 32,
"height": 32
}
}
and I need this set of classes:
public class Rootobject
{
public string id { get; set; }
public string type { get; set; }
public string name { get; set; }
public Image image { get; set; }
public Thumbnail thumbnail { get; set; }
}
public class Image
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class Thumbnail
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
It turns out that Visual Studio will do this AUTOMATICALLY. Who knew?
Just get the JSON on your clipboard and do this. Try it!
