4

我通过使用 Newtonsoft 对列表进行序列化,将 JSON 数组作为字符串,如下所示

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

我需要通过将NULL值删除到另一个 JSONString 来转换这个 JSON,如下所示

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

有没有办法可以为此使用 Newtonsoft,或者我该怎么做。

4

1 回答 1

13

您可以JsonSerializerSettings使用NullValueHandling

var result = JsonConvert.SerializeObject(obj, 
            new JsonSerializerSettings() 
            { 
                NullValueHandling = NullValueHandling.Ignore 
            });
于 2013-05-09T06:36:55.373 回答