views:

105

answers:

1

Hi Friends I am trying to deserialize a hidden control field into a json object the code is as follows Dim settings As New Newtonsoft.Json.JsonSerializerSettings() settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore Return Newtonsoft.Json.JsonConvert.DeserializeObject(Of testContract)(txtHidden.Text, settings) But I am getting the following exception. value cannot be null parameter name s: I even added the following lines but it still does not work out. Please help settings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore settings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore settings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace Thanks Bharath

A: 

I had the same exact error message as I tried calling the same method. Make sure you have a default constructor for your target class (your testContract class).

In C# your class and default constructor would look something like this:

class testContract
{
    string StringProperty;
    int IntegerProperty;

    public testContract()
    {
        // This is your default constructor. Make sure this exists.
        // Do nothing here, or set default values for your properties
        IntegerProperty = -1;
    }

    public testContract(string StringProperty, int IntegerProperty)
    {
        // You can have another constructor that accepts parameters too
        this.StringProperty = StringProperty;
        this.IntegerProperty = IntegerProperty;
    }
}

When JSON.net wants to deserialize a JSON string into an object, it first initializes the object using its default constructor and then starts populating its properties. If it doesn't find a default constructor, it will initialize the objected using whatever other constructor that it can find, but it will pass null to all parameters.

In a nutshell, You should either have a default constructor for your target class, or, your non-default constructor must be able to handle all null parameters.

Arash