C # implements JSON serialization method instances that delete null values

Source: Internet
Author: User
To serialize an object, but if the property of the object is null, we want to remove the property as null, how to deal with it? In fact, the method is very simple, following the script of the small part of Learning C # Json serialization to remove the null value of the method bar

To serialize an object, but if the object's property is null, we want to remove the property as null.

Here I useNewtonsoft.Json.dll

Record serialization and deserialization

JSON string Goto Object


Model model=jsonconvert.deserializeobject<model> (Val);

Convert an object to a JSON-formatted string


String jsonstring = Jsonconvert.serializeobject (obj);

So how to filter out NULL when serializing JSON??


var jsonsetting = new Jsonserializersettings {nullvaluehandling = Nullvaluehandling.ignore};var json = Jsonconvert.serializeobject (data, formatting.indented, jsonsetting);

JsonConvert.SerializeObject(obj);serialization of results directly


"Memberquery": {  "PhoneNumber": "13222222222",  "Name": "Test",  "MF": "Female",  "BirthDate": "01/01/2017" ,  "maritalstatus": null,  "country": null}

Filter out NULL serialization results:


"Memberquery": {"PhoneNumber": "13222222222", "Name": "Test", "MF": "Female", "BirthDate": "01/01/2017"}

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.