Remove Null values in JSON and Update JSON

我们两清 提交于 2019-12-21 20:27:57

问题


I have JSON Array as a string by serializing a list using Newtonsoft as below

[{"ID":"1","Name":"somename","Class":"12","Section":null},{"ID":null,"Name":"somename","Class":"13","Section":null},{"ID":2,"Name":"somename","Class":null,"Section":"A"}]

I need to convert this JSON by removing the NULL values to another JSONString like below

[{"ID":"1","Name":"somename","Class":"12",},{"Name":"somename","Class":"13",},{"ID":2,"Name":"somename","Section":"A"}]

Is there a way I can use Newtonsoft for this or how do i do this.


回答1:


You can use JsonSerializerSettings with NullValueHandling:

var result = JsonConvert.SerializeObject(obj, 
            new JsonSerializerSettings() 
            { 
                NullValueHandling = NullValueHandling.Ignore 
            });


来源:https://stackoverflow.com/questions/16455837/remove-null-values-in-json-and-update-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!