Do you know how to easily get classes from a JSON response?

Last updated by Brady Stroud [SSW] about 1 month ago.See history

When integrating with external Web APIs which return a JSON response, there is a quick and easy way to generate classes to handle that response.

If the API specification is published as per our rule: Do you document your Web API?

You can automatically generate your classes from that specification. See our other rule on how to do this: Do you know the best way to generate your classes from swagger? If the specification isn't publised you need to generate your clases from the response you get from calling the API. Here is a trick to do that.

Execute the request, and copy the text of the JSON response.

8 08 2014 3 41 23 PM compressor

Create a new class in Visual Studio, and Click Edit | Paste Special | Past As JSON Classes and classes will be generated from the JSON in the clipboard.

8 08 2014 3 53 17 PM compressor Figure: Edit | Paste Special | Paste JSON As Classes

8 08 2014 3 56 34 PM compressor Figure: Classes generated from the JSON

The results may need cleaning up a little bit, but its much easier than trying to write them manually.

Drew Robson
We open source. Powered by GitHub