none
How to serialize a class with JSONConvert.SerializeObject to remove empty List and null element in class RRS feed

  • Question

  • Hi,

    I am trying to serialize a class with JSON serializeObject.  If the List class has no data, I would like to remove it instead of having an empty list like below.  I tried below command to remove null but it is leaving an empty list <g class="gr_ gr_1562 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="1562" id="1562">arrary</g> in the JSON output.  

    var rawJSON = JsonConvert.SerializeObject(MyList, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

    Is there an option to remove an empty List and null element? Let's say there is no Style information available, I would like to remove "Style": [] completely.  Right now it is leaving a null List in the JSON output.  Any suggestion would be appreciated.

     "Brand": [
            "Levis"
          ],
     "Style": []
          ],
     "Country": [
        "USA"
          ],
     "Region": [
        "Las Vegas"
         ]

     public class Mylist
            {
                public List<string> Brand { get; set; }
                public List<string> Style { get; set; }
                public List<string> Country { get; set; }
                public List<string> Region { get; set; }

           }

    Thursday, April 26, 2018 2:31 AM

Answers

  • Hi Brandon.Ho,

    This forum is for Visual Studio IDE issues, I suggest you post this thread at Visual C# forum where you'll get better support.

    Thanks for your understanding.

     

    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Saturday, April 28, 2018 9:41 AM
    • Marked as answer by Brandon.Ho Saturday, April 28, 2018 5:30 PM
    Saturday, April 28, 2018 9:41 AM

All replies

  • Hi Brandon.Ho,

    This forum is for Visual Studio IDE issues, I suggest you post this thread at Visual C# forum where you'll get better support.

    Thanks for your understanding.

     

    Regards,

    Fletcher


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Saturday, April 28, 2018 9:41 AM
    • Marked as answer by Brandon.Ho Saturday, April 28, 2018 5:30 PM
    Saturday, April 28, 2018 9:41 AM
  • Hi Fletch,

    I'll post it to the Visual C# forum.

    Thanks for letting me know.

    Brandon.

    Saturday, April 28, 2018 5:31 PM