How to troubleshoot KeyNotFoundException
Asked Answered
S

2

5

I am using the following to deserialize a JSON string into my own 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 a way to find out which key triggered the exception?

A general technique (or recipe) for troubleshooting this type of exception would be most appreciated.

Update: If I remove the [d], I receive 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 again, I am looking for a general technique more than just solving this particular instance.

Stationery answered 23/10, 2012 at 19:38 Comment(0)
L
6

Drop a break point on the Deserialize statement, then use the Immediate Window to type in x["d"].ToString(). This will tell you what that evaluates to, which will equate to the key you are attempting to deserialize.

If you get an error while evaluating that, then your x dictionary does not have "d" as a key, which is another thing you can check.

EDIT: I don't think Daniel was suggesting you remove the ["d"] completely. If you do that, you are passing Deserialize a dictionary when it's pretty clearly expecting a string. This type mismatch is what causes the new error you see. Instead, Daniel is suggesting that the dictionary you have doesn't include "d" as a valid key. Which it may or may not, I'm not sure.

Loredo answered 23/10, 2012 at 19:40 Comment(4)
Great tip, thanks! I'll go ahead and try it. In the meanwhile, see my update in the OP.Stationery
Thanks. When I place the breakpoint and do what you advised, I get the value 'x["d"]' threw an exception of type 'System.Collections.Generic.KeyNotFoundException' for that x["d"].ToString(). So, it looks like it is the source of the problem, but how do I handle a dictionary that doesn't have a d as the first (and only) key?Stationery
@NotSoSharp Dictionaries do not need to have "d" as a key. I would recommend you put the x variable under watch in Visual Studio so you can expand it and look at it as much as you want. In particular you might check the .Count attribute. If it's 0, you don't have any keys at all.Loredo
Success! Turns out the .ToString() was completely misleading (that's what happens when I try to learn from an example). Also turns out that the Dictionary<string, object> x = (Dictionary<string, object>)serializer.DeserializeObject(json); statement was completely unnecessary: All I had to do is pass the raw json string to Deserialize(). I should've known better. Thanks a million.Stationery
S
1

Likely its on your x accessing the value for d.

Swoon answered 23/10, 2012 at 19:40 Comment(1)
By using the d I'm merely following the tip how .NET 3.5+ adds 'D' to the result. If I remove it, I get an even more scary barrage of Invalid JSON primitive exception.Stationery

© 2022 - 2024 — McMap. All rights reserved.