confused about how to use JSON in C#

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2010-06-01T15:15:20Z Indexed on 2010/06/01 15:33 UTC
Read the original article Hit count: 271

Filed under:
|
|
|
|

The answer to just about every single question about using C# with json seems to be "use JSON.NET" but that's not the answer I'm looking for.

the reason I say that is, from everything I've been able to read in the documentation, JSON.NET is basically just a better performing version of the DataContractSerializer built into the .net framework...

Which means if I want to deserialize a JSON string, I have to define the full, strongly-typed class for EVERY request I might have. so if I have a need to get categories, posts, authors, tags, etc, I have to define a new class for every one of these things.

This is fine if I built the client and know exactly what the fields are, but I'm using someone else's api, so I have no idea what the contract is unless I download a sample response string and create the class manually from the JSON string.

Is that the only way it's done? Is there not a way to have it create a kind of hashtable that can be read with json["propertyname"]?

Finally, if I do have to build the classes myself, what happens when the API changes and they don't tell me (as twitter seems to be notorious for doing)? I'm guessing my entire project will break until I go in and update the object properties...

So what exactly is the general workflow when working with JSON? And by general I mean library-agnostic. I want to know how it's done in general, not specifically to a target library...

I hope that made sense, this has been a very confusing area to get into...

thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET