1

What am I doing wrong here?

using System.Web.Script.Serialization;

string json = "{\"numbers\":[{\"one\":\"1\"},{\"two\":\"2\"},{\"three\":\"3\"}]}";
dynamic dictionary = new JavaScriptSerializer().Deserialize<dynamic>(json);
Assert.AreEqual(3, dictionary["numbers"].Count);

System.NullReferenceException : Object reference not set to an instance of an object
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToObject (IDictionary`2 dict, System.Type type) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType (System.Type type, System.Object obj) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToList (System.Collections.ArrayList col, System.Type type) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType (System.Type type, System.Object obj) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToObject (IDictionary`2 dict, System.Type type) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType (System.Type type, System.Object obj) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToType[Object] (System.Object obj) [0x00000] in <filename unknown>:0 
  at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[Object] (System.String input) [0x00000] in <filename unknown>:0
3
  • Which line is throwing the error? Commented May 7, 2013 at 23:48
  • Just one thing that I don't get: why are you using the dynamic keyword there, it bring no benefit in your case. Commented May 8, 2013 at 0:02
  • and if you look at this line: "at System.Web.Script.Serialization.JavaScriptSerializer.ConvertToObject (IDictionary`2 dict, System.Type type) [0x00000] in <filename unknown>:0", the error seems to be related when converting to the dictionary. Did you try using the static type instead just to narrow the problem? Commented May 8, 2013 at 0:04

2 Answers 2

2

As I see no problems with your code, apart from the use of the count property, and since the code is breaking when the json is being deserialized, I would advise you to use the static type definition instead, just to narrow the changes of problems with your code:

        string json = "{\"numbers\":[{\"one\":\"1\"},{\"two\":\"2\"},{\"three\":\"3\"}]}";
        var dictionary = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);

Maybe just maybe there is an issue with using dynamic types with the JavaScriptSerializer and if there is indeed a problem with the JavaScriptSerializer you can always report that to MSFT.

Sign up to request clarification or add additional context in comments.

4 Comments

Using <Dictionary<string, object>> over dynamic still gives me the same error :(
I tried that code myself and if does work properly, and even if the JSON was not valid you would get a different exception. I've got not clue what the problem is then. Is that piece of code being use by multiple threads? I can't think of anything else.
There's nothing special about this code, I'm reproducing the issue with a simple NUnit test case. Now I'm using Xamarin Studio 4.0, I wonder if Mono's implementation is buggy or whatever.
Ok that says it all, yes I would say that mono has an implementation problem, I thought you were using .Net (windows)
1

Since I could not make Dictionary<string, object> work, I've decided to drop it and use static types instead. BTW, this online tool is quite handy for generating C# classes from JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.