Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使Json序列化忽略字典键_C#_Json_Dictionary_Serialization_Json.net - Fatal编程技术网

C# 如何使Json序列化忽略字典键

C# 如何使Json序列化忽略字典键,c#,json,dictionary,serialization,json.net,C#,Json,Dictionary,Serialization,Json.net,我正在尝试序列化类中的词典,CustomAttributes词典中的键正在格式化,即使我已将ProcessDictionaryKeys参数设置为false 我添加了[JsonProperty],如下所示: [JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })] public IDictionary&l

我正在尝试序列化类中的词典,
CustomAttributes
词典中的键正在格式化,即使我已将
ProcessDictionaryKeys
参数设置为false

我添加了
[JsonProperty]
,如下所示:

[JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy), NamingStrategyParameters = new object[] { false, false })]
public IDictionary<string, string> CustomAttributes { get; set; }
如您所见,每个字典键的第一个字母都未大写。我怎样才能阻止这种事情发生


编辑:将
ProcessDictionaryKeys
参数更改为true似乎没有任何区别。

问题不会像demo fiddle#1中显示的那样,仅使用问题中的代码重现

相反,您必须使用某些全局序列化程序设置进行序列化,例如:

演示小提琴2

假设这是正确的,
[JsonProperty(NamingStrategyType=typeof(SnakeCaseNamingStrategy),NamingStrategyParameters=new object[]{false,false})]
不会导致字典键被逐字序列化的原因是它只适用于属性名称本身(此处
CustomAttributes
)不是属性的项的属性名称。如果要对属性的项应用命名策略,则需要类似于
ItemNamingStrategyType
——但没有此类功能

那么,你有什么选择

  • 您可以修改全局命名策略以逐字序列化字典名称,如中所示

  • 您可以子类化
    字典
    ,并对其应用,如中所示:

    并按如下方式应用:

    [JsonConverter(typeof(VerbatimDictionaryConverter<string, string>))]
    public IDictionary<string, string> CustomAttributes { get; set; }       
    
    [JsonConverter(typeof(VerbatimDictionaryConverter))]
    公共IDictionary自定义属性{get;set;}
    
    小提琴示范曲#4


  • 删除这个
    NamingStrategyType=typeof(SnakeCaseNamingStrategy)
    ?所有这一切只是将字典的名称更改为默认的camelCase:
    customAttributes\:{“custom Attribute 1\”:“1\”,“customAttribute 2\”:“2
    是的,但这是你的问题,不是吗?“如您所见,每个字典键的第一个字母都未大写。我如何才能阻止这种情况发生?”不,我的问题与字典中的键有关,而不是字典名称。我需要它们不格式化,即保留为“自定义属性1”,而不是格式化为“自定义属性1”
    custom_attributes\":{\"custom Attribute 1\":\"1\",\"customAttribute 2\":\"2\"
    
    var settings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
    };
    var json = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
    
    [JsonDictionary(NamingStrategyType = typeof(DefaultNamingStrategy))]
    public class VerbatimDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
    }
    
    CustomAttributes = new VerbatimDictionary<string, string>()
    {
        {"Custom Attribute 1", "1"},
        {"CustomAttribute 2", "2"}
    }
    
    public class VerbatimDictionaryConverter<TKey, TValue> : JsonConverter<IDictionary<TKey, TValue>>
    {
        [JsonDictionary(NamingStrategyType = typeof(DefaultNamingStrategy))]
        class VerbatimDictionarySerializationSurrogate : IReadOnlyDictionary<TKey, TValue>
        {
            readonly IDictionary<TKey, TValue> dictionary;
    
            public VerbatimDictionarySerializationSurrogate(IDictionary<TKey, TValue> dictionary) 
            { 
                if (dictionary == null) 
                    throw new ArgumentNullException(nameof(dictionary));
                this.dictionary = dictionary; 
            }
            public bool ContainsKey(TKey key) { return dictionary.ContainsKey(key); }
            public bool TryGetValue(TKey key, out TValue value) { return dictionary.TryGetValue(key, out value); }
            public TValue this[TKey key] { get { return dictionary[key]; } }
            public IEnumerable<TKey> Keys { get { return dictionary.Keys; } }
            public IEnumerable<TValue> Values { get { return dictionary.Values; } }
            public int Count { get { return dictionary.Count; } }
            public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return dictionary.GetEnumerator(); }
            IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
        }
    
        public override void WriteJson(JsonWriter writer, IDictionary<TKey, TValue> value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, new VerbatimDictionarySerializationSurrogate(value));
        }
    
        public override bool CanRead { get { return false; } }
    
        public override IDictionary<TKey, TValue> ReadJson(JsonReader reader, Type objectType, IDictionary<TKey, TValue> existingValue, bool hasExistingValue, JsonSerializer serializer) { throw new NotImplementedException(); }
    }
    
    [JsonConverter(typeof(VerbatimDictionaryConverter<string, string>))]
    public IDictionary<string, string> CustomAttributes { get; set; }