5

I am trying to deserialize some JSON that I am grabbing from an asmx service into a list of objects. All of the fields in the class match fields in the JSON, the JSON is coming back valid, but I get the seemingly cryptic error:

Value cannot be null. Parameter name: type.

There is no parameter named type in any of my objects. Has anyone seen this before?

Here is the code that is throwing the error.

System.Web.Script.Serialization.JavaScriptSerializer serr = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Rejection> l = serr.Deserialize<List<Rejection>>(json);

json is a string declared earlier and coming back with valid json that matches the field in my class. Does the class you are deserializing to's name have to match what is in the __type attribute in the json?

1
  • can you post some code snippet? Commented Oct 7, 2009 at 19:09

3 Answers 3

3

I've just got this too - I believe that its something to do with the serialiser being initialised without a type resolver:

// The following fails
var serialiser = new JavaScriptSerializer();
MyClass obj = serialiser.Deserialize<MyClass>(input);

// But the following works fine
var serialiser = new JavaScriptSerializer(new SimpleTypeResolver());
MyClass obj = serialiser.Deserialize<MyClass>(input);

I found that I only got this error when deserialising JSON that had the __type attribute present (which is only present when serialised using a type resolver). If your JSON doesn't have the __type attribute, then deserialisation appears to work fine using either of the above.

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

4 Comments

I'm having same problem as OP, trying this gives me error Operation is not valid due to the current state of the object.
@Shredder I'm going to guess that exception is another separate problem that you only see once you have fixed the first problem.
Yeah, if I remember correctly, I resolved it by parsing the __type out of the json string.
Incase you still have this issue I think you need to use JSON.stringify(yourObj) to prepare your string before sending it to the web service. I had the same error message (..current state of the object) and resolved it using this method
0

I'm not sure exactly where is your problem, but try following code:


string input = "..."; // your asmx data
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<YourCustomClass> novos = new List<YourCustomClass>(
    serializer.Deserialize<YourCustomClass[]>(input)));

Comments

0

I solved my problem by avoiding the javascript serializer all together and using the json.net library. Worked like a charm.

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.