在解除集合时,如何让json.net排除空值?

Con*_*ern 9 c# json json.net

我有JSON,我回来可能会返回null作为值的一部分.我怎么能,或者甚至有办法从集合中排除那些空值?

      {
     "id": "5551212",
     "from": {
        "name": "Message creator",
        "start_time": "2011-10-21T22:00:00",
        "end_time": "2011-10-23T17:00:00",
        "location": "area 51",
        "id": "2121212122"
     },
     "to": {
        "data": [
          {
              "name": "Jay-Z",
              "id": "77777"
           },
           {
              "name": "Bill Murray",
              "id": "88888"
           },
           null,
           {
              "name": "Anthony Hopkins",
              "id": "99999"
           }
        ]
     },
     "message": "Some message from somewhere",
     "updated_time": "2011-09-19T23:53:51+0000",
     "unread": 1,
     "unseen": 0
  }
Run Code Online (Sandbox Code Playgroud)

Bill Murray和Anthony Hopkins之间的通知是返回的null.谢谢.

Kor*_*yem 12

您可以简单地装饰可以为null的属性/字段

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

有关如何减少json大小的更多信息:http://james.newtonking.com/archive/2009/10/23/efficient-json-with-json-net-reducing-serialized-json-size.aspx