简体   繁体   English

序列化 KeyValuePair<TKey, TValue> 在 C# 中将属性类型转换为 JSON

[英]Serializing KeyValuePair<TKey, TValue> type property to JSON in C#

I'm trying to serialize a KeyValuePair property in C# that looks like this:我正在尝试在 C# 中序列化一个 KeyValuePair 属性,如下所示:

[JsonDisplayName("custom")]
public  KeyValuePair<string,string> Custom { get; set; }

to JSON by setting the property with:通过将属性设置为 JSON:

MyClass.Custom = new KeyValuePair<string, string>("destination", destination);

But the output I get looks something like this:但我得到的输出看起来像这样:

"custom":{"Key":"destination","Value":"Paris"}

Instead I want:相反,我想要:

"custom":{"destination":"Paris"}

Any ideas how?有什么想法吗? I'm using Compact Framework and Visual Studio 2008 so I prefer not to use any external library.我正在使用 Compact Framework 和 Visual Studio 2008,所以我不想使用任何外部库。 You're help is greatly appreciated.非常感谢您的帮助。

Update: I have to use my company's Model class which has a SetCustom method that throws an exception if I use a dictionary.更新:我必须使用我公司的 Model 类,它有一个 SetCustom 方法,如果我使用字典,则会引发异常。

You can use dictionary instead of key value pair您可以使用字典而不是键值对

public class A
{
    [JsonProperty("custom")]
    public Dictionary<string, string> Custom
    {
        get;
        set;
    }
}
public class Program
{
    public static void Main()
    {
        A custom = new A();
        custom.Custom = new Dictionary<string, string>(){
            {"destination1", "foo"},
            {"destination2", "bar"},
        };
        Console.WriteLine(JsonConvert.SerializeObject(custom));
    }
}

This will produce这将产生

{"custom":{"destination1":"foo","destination2":"bar"}}

Or if you want to stick with KeyValuePair you will need to create your own converter或者,如果您想坚持使用KeyValuePair ,则需要创建自己的转换器

public class A
{
    [JsonProperty("custom")]
    public KeyValuePair<string, string> Custom
    {
        get;
        set;
    }
}

class KeyValueStringPairConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        KeyValuePair<string, string> item = (KeyValuePair<string, string>)value;
        writer.WriteStartObject();
        writer.WritePropertyName(item.Key);
        writer.WriteValue(item.Value);
        writer.WriteEndObject();
    }

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

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

public class Program
{
    public static void Main()
    {
        A custom = new A();
        JsonSerializerSettings settings = new JsonSerializerSettings{Converters = new[]{new KeyValueStringPairConverter()}};
        custom.Custom = new KeyValuePair<string, string>("destination", "foo");
        Console.WriteLine(JsonConvert.SerializeObject(custom, settings));
    }
}

Don't forget download from NuGet Newtonsoft.Json不要忘记从 NuGet Newtonsoft.Json 下载

class Program
{
    static void Main(string[] args)
    {
        String [,] arr = new String[1,2];
        arr[0,0] = "Hello";
        arr[0,1] = "World";

        Console.WriteLine(JsonConvert.SerializeObject(arr));
        Console.ReadKey(true);
        //[["Hello","World"]]
    }
}

I had the same question as the OP and Bob's answer did the trick for me.我和OP有同样的问题,鲍勃的回答对我有用。 Just wanted to share that the final code differs a bit if you're using System.Text.Json.Serialization in .NET Core / .NET 5+ instead of Newtonsoft.Json.只是想分享一下,如果您在 .NET Core / .NET 5+ 中使用 System.Text.Json.Serialization 而不是 Newtonsoft.Json,则最终代码会有所不同。

public class StringKeyValuePairConverter : JsonConverter<KeyValuePair<string, string>>
{
    public override KeyValuePair<string, string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, KeyValuePair<string, string> value, JsonSerializerOptions options)
    {
        writer.WriteStartObject();
        writer.WritePropertyName(value.Key);
        writer.WriteStringValue(value.Value);
        writer.WriteEndObject();
    }
}

It's mostly the same, except value is strongly typed, eliminating the need for a cast, and WriteStringValue (or the appropriate type-specific variant) is used instead of WriteValue .它几乎是相同的,除了value是强类型的,消除了强制转换的需要,并且使用WriteStringValue (或适当的特定于类型的变体)代替WriteValue

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

相关问题 Json.NET 序列化字典<TKey, TValue>对比键值对<TKey, TValue> - Json.NET serializing Dictionary<TKey, TValue> vs KeyValuePair<TKey, TValue> C#-序列化自定义词典 <TKey,TValue> 对象和TValue子对象 - C# - Serializing Custom Dictionary<TKey,TValue> Object & TValue SubObjects 使用字典<TKey,TValue> (IEnumerable <KeyValuePair<TKey,TValue> &gt;) C# 中的构造函数 - Using Dictionary<TKey,TValue>(IEnumerable<KeyValuePair<TKey,TValue>>) Constructor in C# 物业KeyValuePair <TKey, Tvalue> .Value没有二传手 - The property KeyValuePair<TKey, Tvalue>.Value has no setter 如何访问词典 <TKey, TValue> C#中的.Item属性 - How to access Dictionary<TKey, TValue>.Item Property in C# C#:执行SortedList <TKey,TValue> 实现IReadOnlyDictionary <TKey,TValue> ? - C#: does SortedList<TKey,TValue> implement IReadOnlyDictionary<TKey,TValue>? 为什么IDictionary <TKey,TValue>扩展ICollection <KeyValuePair <TKey,TValue >>? - Why does IDictionary<TKey, TValue> extend ICollection<KeyValuePair<TKey, TValue>>? C#中的泛型,字典<TKey,TValue> - Generics in C#, Dictionary<TKey,TValue> 序列化自定义BindableDictionary <TKey,TValue> ? - Serializing A Custom BindableDictionary<TKey,TValue>? 将字符串的值转换为KeyValuePair的变量<TKey,TValue> - Converting the value of a string to a variable of KeyValuePair<TKey,TValue>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM