json.net所需的属性JSON中未找到 [英] json.net required property not found in json
问题描述
我使用Json.net,我得到了一类如下
公共类RecordAlias
{
[JsonProperty(属性名=开斋节,必需= Required.Always)]
公共字符串ENTITYID {搞定;组; }
[JsonProperty(属性名=援助,要求= Required.AllowNull)]
公共字符串AliasId {搞定;组; }
[JsonProperty(属性名=ISD,要求= Required.AllowNull)]
公共BOOL IsSelected {搞定;组; }
}
因此,继JSON甚至可以通过一些项目反序列化没有财产JSON字符串ISD,我希望该类型的默认值应填充如果不存在,例如,IsSelected应该是除了最后一个项目
<$ p $假p>
[{
开斋节:30022004,
援助:1
},{
开斋节:30021841 ,
援助:1
},{
开斋节:30021848,
援助:1
ISD :真正的
}
]
任何想法,我怎么能做到这一点? / p>
非常感谢
明
您不是在你的JSON字符串指定 EID
,它的设置是必要的。你传递摆脱
...这是一回事吗?
如果我不理解的问题,请让我知道,我会更新我的答案
更新:乙你说明$ b $的 ISD
属性是 AllowNull
。您还必须指定这个属性在你的JSON字符串的值,但它可以为null。每JSON.NET 规格:
该属性必须在JSON定义,但可以是一个空值。
块引用>
您需要指定一个值
ISD
...或标记ISD
在JsonProperty
属性为默认值
。对于默认值
中,规范说:
,则不需要属性。默认状态。
块引用>
[JsonProperty(属性名=ISD,要求= Required.Default )
公共BOOL IsSelected {搞定;组; }
我希望这有助于。
I am using Json.net, I got a class as following
public class RecordAlias { [JsonProperty(PropertyName = "eId", Required = Required.Always)] public string EntityId { get; set; } [JsonProperty(PropertyName = "aId", Required = Required.AllowNull)] public string AliasId { get; set; } [JsonProperty(PropertyName = "iSd", Required = Required.AllowNull)] public bool IsSelected { get; set; } }
So that following json can be deserialized even through some items don't have property "iSd" in json string, I would expect a default value of that type should be populated if not present, for example, IsSelected should be false except last item
[{ "eId" : "30022004", "aId" : "1" }, { "eId" : "30021841", "aId" : "1" }, { "eId" : "30021848", "aId" : "1" "iSd" : true } ]
Any idea how can I achieve this?
Many thanks
Ming
解决方案You're not specifying
eId
in your JSON string, and it's set to be required. You're passingrId
...is this the same thing?If I'm not understanding the question, please let me know and I'll update my answer.
UPDATE: You're indicating that the
iSd
property isAllowNull
. You still have to specify a value for this property in your JSON string, but it can be null. Per the JSON.NET specs:The property must be defined in JSON but can be a null value.
You need to specify a value for
iSd
...or markiSd
in yourJsonProperty
attribute asDefaultValue
. ForDefaultValue
, the spec says:The property is not required. The default state.
[JsonProperty(PropertyName = "iSd", Required = Required.Default)] public bool IsSelected { get; set; }
I hope this helps.
这篇关于json.net所需的属性JSON中未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!