Last active
March 25, 2024 07:14
-
-
Save primaryobjects/e1141ee56509c4a51666f4fe52b1c5dd to your computer and use it in GitHub Desktop.
Neat trick to convert JSON to a C# class in Visual Studio.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Copy some JSON to your clipboard. Need some JSON? Here you go http://goo.gl/mWZSEL | |
2. In Visual Studio, right-click your project and select Add->Class. | |
3. Highlight the following code in your class: | |
class Class1 | |
{ | |
} | |
4. From the top menu, select Edit->Paste Special->Paste JSON as Classes. | |
5. Rename "Rootobject" to your desired class name. Done! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ClassLibrary1 | |
{ | |
public class Rootobject | |
{ | |
public string swagger { get; set; } | |
public Info info { get; set; } | |
public string host { get; set; } | |
public string basePath { get; set; } | |
public string[] schemes { get; set; } | |
public string[] consumes { get; set; } | |
public string[] produces { get; set; } | |
public Paths paths { get; set; } | |
public Definitions definitions { get; set; } | |
} | |
public class Info | |
{ | |
public string version { get; set; } | |
public string title { get; set; } | |
public string description { get; set; } | |
public string termsOfService { get; set; } | |
public Contact contact { get; set; } | |
public License license { get; set; } | |
} | |
public class Contact | |
{ | |
public string name { get; set; } | |
} | |
public class License | |
{ | |
public string name { get; set; } | |
} | |
public class Paths | |
{ | |
public Pets pets { get; set; } | |
public PetsId petsid { get; set; } | |
} | |
public class Pets | |
{ | |
public Get get { get; set; } | |
public Post post { get; set; } | |
} | |
public class Get | |
{ | |
public string description { get; set; } | |
public string operationId { get; set; } | |
public string[] produces { get; set; } | |
public Parameter[] parameters { get; set; } | |
public Responses responses { get; set; } | |
} | |
public class Responses | |
{ | |
public _200 _200 { get; set; } | |
public Default _default { get; set; } | |
} | |
public class _200 | |
{ | |
public string description { get; set; } | |
public Schema schema { get; set; } | |
} | |
public class Schema | |
{ | |
public string type { get; set; } | |
public Items items { get; set; } | |
} | |
public class Items | |
{ | |
public string _ref { get; set; } | |
} | |
public class Default | |
{ | |
public string description { get; set; } | |
public Schema1 schema { get; set; } | |
} | |
public class Schema1 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Parameter | |
{ | |
public string name { get; set; } | |
public string _in { get; set; } | |
public string description { get; set; } | |
public bool required { get; set; } | |
public string type { get; set; } | |
public Items1 items { get; set; } | |
public string collectionFormat { get; set; } | |
public string format { get; set; } | |
} | |
public class Items1 | |
{ | |
public string type { get; set; } | |
} | |
public class Post | |
{ | |
public string description { get; set; } | |
public string operationId { get; set; } | |
public string[] produces { get; set; } | |
public Parameter1[] parameters { get; set; } | |
public Responses1 responses { get; set; } | |
} | |
public class Responses1 | |
{ | |
public _2001 _200 { get; set; } | |
public Default1 _default { get; set; } | |
} | |
public class _2001 | |
{ | |
public string description { get; set; } | |
public Schema2 schema { get; set; } | |
} | |
public class Schema2 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Default1 | |
{ | |
public string description { get; set; } | |
public Schema3 schema { get; set; } | |
} | |
public class Schema3 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Parameter1 | |
{ | |
public string name { get; set; } | |
public string _in { get; set; } | |
public string description { get; set; } | |
public bool required { get; set; } | |
public Schema4 schema { get; set; } | |
} | |
public class Schema4 | |
{ | |
public string _ref { get; set; } | |
} | |
public class PetsId | |
{ | |
public Get1 get { get; set; } | |
public Delete delete { get; set; } | |
} | |
public class Get1 | |
{ | |
public string description { get; set; } | |
public string operationId { get; set; } | |
public string[] produces { get; set; } | |
public Parameter2[] parameters { get; set; } | |
public Responses2 responses { get; set; } | |
} | |
public class Responses2 | |
{ | |
public _2002 _200 { get; set; } | |
public Default2 _default { get; set; } | |
} | |
public class _2002 | |
{ | |
public string description { get; set; } | |
public Schema5 schema { get; set; } | |
} | |
public class Schema5 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Default2 | |
{ | |
public string description { get; set; } | |
public Schema6 schema { get; set; } | |
} | |
public class Schema6 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Parameter2 | |
{ | |
public string name { get; set; } | |
public string _in { get; set; } | |
public string description { get; set; } | |
public bool required { get; set; } | |
public string type { get; set; } | |
public string format { get; set; } | |
} | |
public class Delete | |
{ | |
public string description { get; set; } | |
public string operationId { get; set; } | |
public Parameter3[] parameters { get; set; } | |
public Responses3 responses { get; set; } | |
} | |
public class Responses3 | |
{ | |
public _204 _204 { get; set; } | |
public Default3 _default { get; set; } | |
} | |
public class _204 | |
{ | |
public string description { get; set; } | |
} | |
public class Default3 | |
{ | |
public string description { get; set; } | |
public Schema7 schema { get; set; } | |
} | |
public class Schema7 | |
{ | |
public string _ref { get; set; } | |
} | |
public class Parameter3 | |
{ | |
public string name { get; set; } | |
public string _in { get; set; } | |
public string description { get; set; } | |
public bool required { get; set; } | |
public string type { get; set; } | |
public string format { get; set; } | |
} | |
public class Definitions | |
{ | |
public Pet Pet { get; set; } | |
public Newpet NewPet { get; set; } | |
public Errormodel ErrorModel { get; set; } | |
} | |
public class Pet | |
{ | |
public string type { get; set; } | |
public Allof[] allOf { get; set; } | |
} | |
public class Allof | |
{ | |
public string _ref { get; set; } | |
public string[] required { get; set; } | |
public Properties properties { get; set; } | |
} | |
public class Properties | |
{ | |
public Id id { get; set; } | |
} | |
public class Id | |
{ | |
public string type { get; set; } | |
public string format { get; set; } | |
} | |
public class Newpet | |
{ | |
public string type { get; set; } | |
public string[] required { get; set; } | |
public Properties1 properties { get; set; } | |
} | |
public class Properties1 | |
{ | |
public Name name { get; set; } | |
public Tag tag { get; set; } | |
} | |
public class Name | |
{ | |
public string type { get; set; } | |
} | |
public class Tag | |
{ | |
public string type { get; set; } | |
} | |
public class Errormodel | |
{ | |
public string type { get; set; } | |
public string[] required { get; set; } | |
public Properties2 properties { get; set; } | |
} | |
public class Properties2 | |
{ | |
public Code code { get; set; } | |
public Message message { get; set; } | |
} | |
public class Code | |
{ | |
public string type { get; set; } | |
public string format { get; set; } | |
} | |
public class Message | |
{ | |
public string type { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"swagger": "2.0", | |
"info": { | |
"version": "1.0.0", | |
"title": "Swagger Petstore", | |
"description": "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", | |
"termsOfService": "http://swagger.io/terms/", | |
"contact": { | |
"name": "Swagger API Team" | |
}, | |
"license": { | |
"name": "MIT" | |
} | |
}, | |
"host": "petstore.swagger.io", | |
"basePath": "/api", | |
"schemes": [ | |
"http" | |
], | |
"consumes": [ | |
"application/json" | |
], | |
"produces": [ | |
"application/json" | |
], | |
"paths": { | |
"/pets": { | |
"get": { | |
"description": "Returns all pets from the system that the user has access to", | |
"operationId": "findPets", | |
"produces": [ | |
"application/json", | |
"application/xml", | |
"text/xml", | |
"text/html" | |
], | |
"parameters": [ | |
{ | |
"name": "tags", | |
"in": "query", | |
"description": "tags to filter by", | |
"required": false, | |
"type": "array", | |
"items": { | |
"type": "string" | |
}, | |
"collectionFormat": "csv" | |
}, | |
{ | |
"name": "limit", | |
"in": "query", | |
"description": "maximum number of results to return", | |
"required": false, | |
"type": "integer", | |
"format": "int32" | |
} | |
], | |
"responses": { | |
"200": { | |
"description": "pet response", | |
"schema": { | |
"type": "array", | |
"items": { | |
"$ref": "#/definitions/Pet" | |
} | |
} | |
}, | |
"default": { | |
"description": "unexpected error", | |
"schema": { | |
"$ref": "#/definitions/ErrorModel" | |
} | |
} | |
} | |
}, | |
"post": { | |
"description": "Creates a new pet in the store. Duplicates are allowed", | |
"operationId": "addPet", | |
"produces": [ | |
"application/json" | |
], | |
"parameters": [ | |
{ | |
"name": "pet", | |
"in": "body", | |
"description": "Pet to add to the store", | |
"required": true, | |
"schema": { | |
"$ref": "#/definitions/NewPet" | |
} | |
} | |
], | |
"responses": { | |
"200": { | |
"description": "pet response", | |
"schema": { | |
"$ref": "#/definitions/Pet" | |
} | |
}, | |
"default": { | |
"description": "unexpected error", | |
"schema": { | |
"$ref": "#/definitions/ErrorModel" | |
} | |
} | |
} | |
} | |
}, | |
"/pets/{id}": { | |
"get": { | |
"description": "Returns a user based on a single ID, if the user does not have access to the pet", | |
"operationId": "findPetById", | |
"produces": [ | |
"application/json", | |
"application/xml", | |
"text/xml", | |
"text/html" | |
], | |
"parameters": [ | |
{ | |
"name": "id", | |
"in": "path", | |
"description": "ID of pet to fetch", | |
"required": true, | |
"type": "integer", | |
"format": "int64" | |
} | |
], | |
"responses": { | |
"200": { | |
"description": "pet response", | |
"schema": { | |
"$ref": "#/definitions/Pet" | |
} | |
}, | |
"default": { | |
"description": "unexpected error", | |
"schema": { | |
"$ref": "#/definitions/ErrorModel" | |
} | |
} | |
} | |
}, | |
"delete": { | |
"description": "deletes a single pet based on the ID supplied", | |
"operationId": "deletePet", | |
"parameters": [ | |
{ | |
"name": "id", | |
"in": "path", | |
"description": "ID of pet to delete", | |
"required": true, | |
"type": "integer", | |
"format": "int64" | |
} | |
], | |
"responses": { | |
"204": { | |
"description": "pet deleted" | |
}, | |
"default": { | |
"description": "unexpected error", | |
"schema": { | |
"$ref": "#/definitions/ErrorModel" | |
} | |
} | |
} | |
} | |
} | |
}, | |
"definitions": { | |
"Pet": { | |
"type": "object", | |
"allOf": [ | |
{ | |
"$ref": "#/definitions/NewPet" | |
}, | |
{ | |
"required": [ | |
"id" | |
], | |
"properties": { | |
"id": { | |
"type": "integer", | |
"format": "int64" | |
} | |
} | |
} | |
] | |
}, | |
"NewPet": { | |
"type": "object", | |
"required": [ | |
"name" | |
], | |
"properties": { | |
"name": { | |
"type": "string" | |
}, | |
"tag": { | |
"type": "string" | |
} | |
} | |
}, | |
"ErrorModel": { | |
"type": "object", | |
"required": [ | |
"code", | |
"message" | |
], | |
"properties": { | |
"code": { | |
"type": "integer", | |
"format": "int32" | |
}, | |
"message": { | |
"type": "string" | |
} | |
} | |
} | |
} | |
} |
Wow, I can't beleive I didn't know about this. Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent example, yes, we can easily convert JSON to C# class using paste special in VS
If anyone want to convert it using online tools check Online JSON to C# Class Converter