繁体   English   中英

Json.net忽略子对象的空值

[英]Json.net ignore null values of child objects

我是json.net的新用户,因此将不胜感激。

我正在使用json.net的net 2.0版本,因为我需要将其集成到Unity3d引擎中建议的答案(使用nullvaluehandling)只是行不通! 我应该认为这是对最旧版本的限制吗?

因此,有一个像这样的对象:

Object ()
{
   ChildObject ChildObject1;
   ChildObject ChildObject2;
}

ChildObject ()
{
   Property1;
   Property2;
}

我希望json.net 仅序列化所有对象(包括子对象)的非null属性 因此,如果我仅声明ChildObject1的property1和ChildObject2的property2,则JsonConvert的字符串将如下所示:

{
  "ChildObject1": 
        {
           "Property1": "10"
        },
  "ChildObject1": 
        {
          "Property2":"20"
        }
}

但是默认行为会创建如下字符串:

{
  "ChildObject1": 
           {
             "Property1": "10", 
             "Property2": "null" 
           },
  "ChildObject2": 
           {
             "Property1": "null, 
             "Property2":"20"
           }
}

我知道NullValueHandling,但是在我看来,它不能正确地工作! 它仅忽略父对象(我们正在序列化的对象)的null属性,但是如果此父对象有一些子对象,则不会忽略此子对象的null属性。 我的情况不同。

如果子对象的一个​​属性甚至都不为空,则将其序列化为一个具有非空属性的字符串,而其他属性为空。


我的代码的UPD示例:

我有嵌套的课程

我的对象是这样的:

public class SendedMessage
{
    public List<Answer> Answers { get; set; }
    public AnswerItem Etalon {get; set;}
}

public class Answer () 
{
public AnswerItem Item { get; set; }
}

   public class AnswerItem () 
   {
   public string ID { get; set; }
   public bool Result { get; set; }
   public int Number { get; set; }
   }

我像这样设置了一个SendedMessage对象:

new SendedMessage x;
x.Etalon = new AnswerItem();
x.Etalon.Result = true;

比我用的

string ignored = JsonConvert.SerializeObject(x,
          Formatting.Indented,
          new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

它使这个字符串:

{“ Etalon”:{“ ID” =“ null”,“ Result” =“ false”,Number“ =” null“}}

因此,它实际上会忽略空对象的Answers(一个List),但不要忽略子对象的null属性。

感谢帮助!

用这个 :

string ignored = sonConvert.SerializeObject(movie,
        Formatting.Indented,
        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

这是示例:

public class Movie
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Classification { get; set; }
    public string Studio { get; set; }
    public DateTime? ReleaseDate { get; set; }
    public List<string> ReleaseCountries { get; set; }
}

static void Main(string[] args)
{
    Movie movie = new Movie();
    movie.Name = "Bad Boys III";
    movie.Description = "It's no Bad Boys";

    string included = JsonConvert.SerializeObject(movie,
        Formatting.Indented,new JsonSerializerSettings { });

            // {
            //   "Name": "Bad Boys III",
            //   "Description": "It's no Bad Boys",
            //   "Classification": null,
            //   "Studio": null,
            //   "ReleaseDate": null,
            //   "ReleaseCountries": null
            // }

            string ignored = JsonConvert.SerializeObject(movie,
              Formatting.Indented,
              new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

            // {
            //   "Name": "Bad Boys III",
            //   "Description": "It's no Bad Boys"
            // }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM