Just trying to create an anonymous type from JSON without knowing anything about the parameters ahead of time, and fully interpreting them (possibly with hints). i.e. that value "looks" like an int, string, or date.
The only way that I know of so far is to create an anonymous type that you have pre-knowledge of. See the .DeserializeAnonymousType(...) method below.
Can anyone do better than this? Thanks.
var jsonString = "{"user_id": 1, "user_type": "moderator", "name": "Fred"}";
JToken root = JObject.Parse(jsonString);
var anonTemplate = new{user_id=0, user_type="", name="" };
var a = JsonConvert.DeserializeAnonymousType(root.ToString(), anonTemplate);
var b = JsonConvert.DeserializeObject<Object>(root.ToString()); // actually turns into a JsonObject which is something differet.
Update
I downloaded dynamicduck and am playing with it a little. Will this weird dynamic "wrapper" idea of Brian's be -able in the ways I need (serializable, etc)?
http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx
http://weblogs.asp.net/britchie/archive/2010/08/03/dynamicduck-duck-typing-in-a-dynamic-world.aspx
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…