2

I have a Json serialized configuration in which I need to deserialize the object using

JsonConvert.DeserializeObject<>(jsonConfig)

to Myclass list. In jsonConfig there may be some properties missing where I got an exception like below.

Required property 'xxx' not found in JSON. Path '[0].yyy',

So is there any way to handle the undefined values while deserializing an object in c#?

4

2 回答 2

1

您需要nullable在对象模型类中创建这些属性,如下所示。

 [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
 public int? someProperty { get; set; }

此外,如果在反序列化时类中缺少属性本身,那么您可以执行以下操作:

 JsonSerializerSettings settings = new JsonSerializerSettings();
 settings.MissingMemberHandling = MissingMemberHandling.Ignore;

 var deserializedObj = JsonConvert.DeserializeObject<MyModelClass>(jsonConfig, settings);
于 2019-09-19T07:49:22.107 回答
0

undefined不是有效的 json 值,即使它在 javascript 中有效。请检查https://api.jquery.com/jQuery.parseJSON/

  • 对于 json,使用 null 而不是undefined: { "something": null }
于 2019-09-19T07:45:35.380 回答