Object JSON转换忽略空对象C#

Object JSON转换忽略空对象C#,object,Object,我试图忽略Json上的空对象 这是我的Json: { “客户”:“测试n测试p”, “firstName”:“test n”, “姓氏”:“测试p”, “更新日期”:“2018-12-28T16:25:25Z”, “导师”:{} } 有人知道如何解决这个问题吗 请让我知道 private class CustomerB2C { public string custName = ""; public string firstName = "";

我试图忽略Json上的空对象

这是我的Json: { “客户”:“测试n测试p”, “firstName”:“test n”, “姓氏”:“测试p”, “更新日期”:“2018-12-28T16:25:25Z”, “导师”:{} }

有人知道如何解决这个问题吗

请让我知道

    private class CustomerB2C
    {
        public string custName = "";
        public string firstName = "";
        public string lastName = "";
        public DateTime updateDate = DateTime.MinValue;
        public Tutor tutor = null;
    }

    private class Tutor
    {
        public string tutorName = "";
    }

    private static void test()
    {
        CustomerB2C customerB2C = new CustomerB2C();
        customerB2C.custName = "Mike Smith";
        customerB2C.firstName = "Mike";
        customerB2C.lastName = "Smith";
        customerB2C.updateDate = DateTime.Now;
        customerB2C.tutor = null; //is already null, setting to null here for demonstration purposes
        string json1 = JsonConvert.SerializeObject(customerB2C);
        string json2 = JsonConvert.SerializeObject(customerB2C, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
    }

到目前为止你有什么?我需要在我的Json中省略tutor:{}当它为空时,不需要发送它而不是这个{“custname”:“test n test p”,“firstName”:“test n”,“lastName”:“test p”,“updatedatedate”:“2018-12-28161:25:25Z”,“tutor”:{}我想要这个{“custname”:“test test n test p”,“firstName”:“test n”,“lastName”:“test p”,“updateDate:“2018-12-28T16:25:25Z”}这是我的代码:string jsonString=Newtonsoft.Json.JsonConvert.SerializeObject(customerB2C,Formatting.Indented,new JsonSerializerSettings{ContractResolver=new skipmptyContractResolver(),DefaultValueHandling=DefaultValueHandling.Ignore});虽然这段代码可能会回答这个问题,但最好解释如何解决问题,并提供代码作为示例或参考。只有代码的答案可能会令人困惑,并且缺乏上下文。这对我不起作用,仍然是json格式中出现的空对象。我修改了我的答案并包含了一个屏幕截图。代码字符串json1包含空的tutor对象,json2忽略了空的tutor对象。这有帮助吗?如果没有,那么我不理解你的问题。