"Value cannot be null" error while json deserializing using C # / JavaScriptSerializer

I am trying to deserialize some JSON that I grab from an asmx service into a list of objects. All fields in the classes correspond to JSON fields, JSON is returned, but I get an apparent cryptic error:

The 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 throws an 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 returning with a valid json that matches the field in my class. Should the class you deserialize for the name match what is in the __type attribute in json?

+5
source share
3

- , - :

// 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);

, JSON, __type ( ). JSON __type, , .

+3

, , :


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

, javascript json.net. .

0

All Articles