空值抛出的异常反序列化空值时JSON.net [英] null value exception thrown when deserializing null value JSON.net
问题描述
喜的朋友我想反序列化一个隐藏的控制领域成JSON对象中的code是如下:
昏暗的设置作为新Newtonsoft.Json.JsonSerializerSettings()
settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
返回Newtonsoft.Json.JsonConvert.DeserializeObject(中testContract)(txtHidden.Text,设置)
不过,我得到以下异常。 值不能为空参数名S:
我甚至增加了以下几行,但它仍然无法正常工作了。请大家帮帮忙。
settings.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore
settings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
settings.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace
我有同样的确切的错误消息,因为我打过电话了同样的方法。请确保您有一个的的默认的构造函数的作为你的目标类(你的 testContract
类)。
在C#类和默认的构造函数会是这个样子:
类testContract
{
串StringProperty;
INT IntegerProperty;
公共testContract()
{
//这是默认的构造函数。确保此存在。
//什么都不做在这里,或者设置默认值的属性
IntegerProperty = -1;
}
公共testContract(字符串StringProperty,INT IntegerProperty)
{
//你可以接受的参数也是另一个构造。
this.StringProperty = StringProperty;
this.IntegerProperty = IntegerProperty;
}
}
当JSON.net要反序列化一个JSON字符串转换成一个对象,它首先初始化使用它的默认构造函数的对象,然后开始填充它的属性。如果它没有找到一个默认的构造函数,它会使用任何其他的构造函数,它可以找到初始化对象,但它会通过空
所有参数。
在简单地说,你要么有一个默认的构造函数为你的目标类别,或者,您的非默认构造函数必须能够处理所有空参数。
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
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 object 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.
这篇关于空值抛出的异常反序列化空值时JSON.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!