繁体   English   中英

Json.NET 忽略字典中的空值

[英]Json.NET Ignore null values in dictionary

使用 JSON.NET 序列化字典时,似乎忽略了NullValueHandling设置。

var dict = new Dictionary<string, string>
{
    ["A"] = "Some text",
    ["B"] = null
};

var json = JsonConvert.SerializeObject(dict, Formatting.Indented,
    new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore
    });

Console.WriteLine(json);

输出:

{
  "A": "Some text",
  "B": null
}

我预计 json 输出中只存在键为“A”的 KVP,而忽略了 KVP“B”。

如何告诉 Json.NET 仅序列化不包含空值的条目?

我只会用 LINQ 从原始字典中过滤掉null值并序列化过滤后的字典:

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace JsonSerialize {
    public static class Program {
        private static Dictionary<string, string> dict = new Dictionary<string, string> {
            ["A"] = "Some text",
            ["B"] = null
        };

        public static void Main(string[] args) {
            var filtered = dict
                .Where(p => p.Value != null)
                .ToDictionary(p => p.Key, p => p.Value);

            var json = JsonConvert.SerializeObject(filtered, Formatting.Indented);

            Console.WriteLine (json);
        }
    }
}

这使:

{
  "A": "Some text"
}

NullValueHandling设置仅适用于类属性而不适用于字典。 JSON.NET 中似乎没有内置方法来忽略字典中的空值。

如果您希望 JSON.NET 为您处理这种情况,那么您可以创建一个自定义 JSON 转换器并覆盖WriteJson方法来处理空值。

public class CustomJsonConverter : JsonConverter
{
    public override bool CanRead => false;

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Dictionary<string, string>);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var dictionary = (Dictionary<string, string>)value;

        writer.WriteStartObject();

        foreach (var pair in dictionary)
        {
            if (pair.Value != null)
            {
                writer.WritePropertyName(pair.Key);

                serializer.Serialize(writer, pair.Value);
            }
        }

        writer.WriteEndObject();
    }
}

然后你可以像这样使用:

var json = JsonConvert.SerializeObject(dict, Formatting.Indented, new CustomJsonConverter());

序列化的主要思想是在反序列化之后,您应该返回相同的对象。 从字典中省略具有null值的键很可能是不合逻辑的,因为键本身代表一些数据。 但是不保存空字段是可以的,因为在反序列化之后你仍然会有相同的对象,因为默认情况下这些字段会被初始化为null

并且,如果它们是null s,这对于类字段可以正常工作。 看看这个例子

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; }
}

Movie movie = new Movie();
movie.Name = "Bad Boys III";
movie.Description = "It's no Bad Boys";

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

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

在您的情况下,某些键的null ,但这与提供的文档中的值不同。

这可能对您有所帮助,但您应该了解ToDictionary如何影响性能:

var json = JsonConvert.SerializeObject(dict.Where(p => p.Value != null)
    .ToDictionary(p => p.Key, p => p.Value), Formatting.Indented);

暂无
暂无

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

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