I use the following to deserialize a JSON string into my class:
JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string, object> x = (Dictionary<string, object>)serializer.DeserializeObject(json); MyJsonStruct data = serializer.Deserialize<MyJsonStruct>(x["d"].ToString());
But as soon as the JavaScriptSerializer.Deserialize() method is called, an exception is thrown:
A first chance exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
Is there any way to find out which key raised the exception?
A common technique (or recipe) for troubleshooting this type would be most appreciated.
Update: If I delete [d] , I get the following:
A first chance exception of type 'System.ArgumentException' occurred in System.Web.Extensions.dll System.ArgumentException: Invalid JSON primitive: System.Collections.Generic.Dictionary. at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
But then again, I am looking for a common technique more than just solving this particular case.
source share