json2csharp generate c# classes from json
developed by Jonathan Keith
with thanks to the JSON C# Class Generator project
and James Newton-King's Json.NET
Feed me { some json }, Seymour!
Parsing your JSON didn't work. Please make sure it's valid.
Already did that? Please let me know so I can fix it.
About
I was initially directed to Antiufo's JSON C# Class Generator project by a comment in a Scott Hanselman blog post. After using the tool for some time, adapting it for the web seemed like a natural progression to increase value and extend its use to the greater development community.
Why
When consuming an external JSON-producing service, it's nice to have locally-defined static types that you can code against. You gain type-safety and intellisense. Json2csharp will do the mundane work of writing classes that match the data provided by such a service.
The advent of dynamic languages has made consuming JSON much easier, although in C# you will not enjoy the added benefit of compile-time checking and intellisense. Also, dynamic typing is only available if you are able to use .NET 4.0+ Framework in your projects. If you'd like to leverage .NET's dynamic types for consuming JSON, I recommend the JsonFx project which provides direct deserialization to dynamic objects.
How
Simple! Type or paste a JSON string or a URL that produces JSON into the large text
area above, then click the Generate button. Here are some examples
you can try:
http://search.twitter.com/search.json?q=c%23
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Schema
Your JSON represents a JavaScript object graph, which is both dynamic and schema-less. To translate this data structure to a static language like C#, the schema must be flattened into types that can handle the most generic case. Because of this, you will get the best results out of json2csharp when your JSON schema is consistent and you may get unexpected, poor, or just plain broken output when that is not the case.
Naming
The rules for identifiers in C# will not accomodate the sequences of characters
that are allowed in JSON. For example, { "0" : "foobar" } is completely
valid JSON, but a C# property with identifier "0" will not compile.
Further, objects or properties in your JSON may have names that conflict with .NET
framework types that may also lead to compiler errors.
Close