JSON convert игнорировать пустой объект C # - PullRequest
0 голосов
/ 28 декабря 2018

Я пытаюсь игнорировать пустой объект на моем Json

это мой Json: {"custNmae": "test n test p", "firstName": "test n", "lastName":" test p "," updateDate ":" 2018-12-28T16: 25: 25Z "," tutor ": {}}

У кого-нибудь есть идеи, как решить эту проблему?

Пожалуйста, дайте мне знать !!!!

1 Ответ

0 голосов
/ 28 декабря 2018
    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 });
    }

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...