本文介绍了Json.net DefaultValueHandling单独排除布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用json.net进行序列化时,我在序列化设置中使用了DefaultValueHandling.Ignore,如果将布尔值设置为false,则会导致删除密钥.我希望仅针对布尔类型免除该税,并申请其他类型和类.请帮助

While serializing using json.net i used DefaultValueHandling.Ignore in serialization settings, which result in removal of key if the bool is set false. I want that to be exempted for type bool alone, and apply for other types and classes. Please help

谢谢.

推荐答案

DefaultValueHandling.Ignore.这是课程:

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Include)]
    public bool IsEmployed { get; set; }
}

让我们说我们有以下示例:

Lets say that we have the following sample:

var person = new Person
            {
                FirstName = "John",
                IsEmployed = false
            };

var json = JsonConvert.SerializeObject(person, new JsonSerializerSettings { DefaultValueHandling = DefaultValueHandling.Ignore });

将导致以下json:

{
    "FirstName": "John",
    "IsEmployed": false
}

这篇关于Json.net DefaultValueHandling单独排除布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:08