Creating C# classes from JSON

January 21, 2014 mgroves 3 comments
Tags: json tools c# visual studio restsharp

I've been working a little bit here and there on a mobile app. One thing this app does a lot is use a RESTful API over HTTP, where the results are JSON.

I'm using RestSharp to handle these requests, and RestSharp can automatically serialize Json results into C# object(s), assuming I have C# objects that match.

Well, some of these JSON results can be a little complex. Not terribly complex, mind you, but enough to where it's annoying to create the classes myself. But I thought to myself, maybe there's a tool out there that already does this for me. I googled around and found json2csharp, which is a handy web app that does exactly that.

Well, thanks to Rob Gibbens, I now know that this feature is actually built-in to Visual Studio 2013, and available for 2012 as part of "ASP.NET and Web Tools".

Paste JSON As Classes in Visual Studio

You can see that there are some differences, but these tools get you most of the way there!

Comments

Avatar for Chris Gardner

Posted by Chris Gardner on January 22, 2014

Grrr, where was this when I was mapping the new Twitter API JSON responses...

Thanks for the tip.

Avatar for mgroves

Posted by mgroves on January 26, 2014

A little update here: if you use the "Paste Special" in Visual Studio, note that it uses arrays. However, if you try to use these on RestSharp, you'll get an error. For RestSharp, you'll need to use List<T>

Avatar for Pete Shearer

Posted by Pete Shearer on January 27, 2014

I really could have used this earlier last week. Glad to know it is there, though. Thanks!

Leave a Comment

*
*
Some HTML will be escaped, you can use StackOverflow-flavored Markdown
Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter