json.net required property not found in json

梦想与她 提交于 2019-12-10 02:50:19

问题


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?


回答1:


You're not specifying eId in your JSON string, and it's set to be required. You're passing rId...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 is AllowNull. 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 mark iSd in your JsonProperty attribute as DefaultValue. For DefaultValue, 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.




回答2:


Make the property a bool not a string.




回答3:


I made a little table for the Required enum values and their effect based on the Required documentation:

                       | Must be present | Can be Null value
-----------------------+-----------------+------------------
Required.Default       |                 |         ✓    
-----------------------+-----------------+------------------
Required.AllowNull     |        ✓        |         ✓    
-----------------------+-----------------+------------------
Required.Always        |        ✓        |            
-----------------------+-----------------+------------------
Required.DisallowNull  |                 |          

In your case the isD is optional you should have used Required.Default (or Required.DisallowNull). Using Required.AllowNull also makes the isD mandatory and it thus throws the Exception when it's missing.

Note that in this case it makes no sense to differentiate between "Optional and may be null" (Required.Default) or "Optional but may not be null" (Required.DisallowNull) because bool is a value type that cannot be null. If you wanted to allow null values you need to use a nullable value type (bool?), however then the default value (when the value is not present) would be null, except when you set it manually (for example to false):

[JsonProperty(PropertyName = "iSd", Required = Required.Default)]
public bool? IsSelected { get; set; } = false;


来源:https://stackoverflow.com/questions/7472486/json-net-required-property-not-found-in-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!