如何正确序列化包含list< keyvaluepair< string,string>>的对象进入杰森?

如何正确序列化包含list< keyvaluepair< string,string>>的对象进入杰森?

问题描述:

我正在尝试序列化一个名为TableDTO的对象的集合.该对象包含名称",日期"和列表">. 我正在尝试使用C#中的Newtonsoft.Json库对其进行序列化

I'm trying to serialize a collection of object named TableDTO. This object contains a Name, Date and a List>. I'm trying to serialize it using Newtonsoft.Json library in C#

当我构造对象时,一切都很好.我这样添加KeyValuePair:
mylist.Add(new KeyValuePair<string, string>($"Col{compteur}", value.Value));
然后将列表添加到我的TableDTO中
TableDTO.List =我的列表
然后我像这样序列化我的对象
JsonConvert.SerializeObject(TableDto);
这就是我得到的

{ "FileName" : "MEP_3_10$3aList.xlsx", "Conditions" :{"Predicate" : "<select a condition>"}, "DataRows" : [{"Key" : "Col1","Value" : "value"}, {"Key" : "Col2","Value" : "value"}] }

Every things works good when i construct the object. I add the KeyValuePair like this :
mylist.Add(new KeyValuePair<string, string>($"Col{compteur}", value.Value));
Then i add the list to my TableDTO
TableDTO.List = mylist
Then i serialize my object like this
JsonConvert.SerializeObject(TableDto);
And here is what i got

{ "FileName" : "MEP_3_10$3aList.xlsx", "Conditions" :{"Predicate" : "<select a condition>"}, "DataRows" : [{"Key" : "Col1","Value" : "value"}, {"Key" : "Col2","Value" : "value"}] }

我遇到的问题是当我序列化它时 而不是拥有

The problem I encountered is when i serialized it Instead of having

{
    {"Col1":"value"},
    {"Col2":"value"}
}

列表像这样被序列化

{
    {"Key" : "Col1","Value" : "value"},
    {"Key" : "Col2","Value" : "value"}
}

我尝试使用stackoverflow上另一篇文章中描述的转换器,但是由于列表是我对象的属性,因此使用起来并不容易.

非常感谢您的帮助

I tried to use a converter as described on another post in stackoverflow but since the list is a property of my object it's not as easy.

Thanks a lot for your help

您遇到的问题是: NewtonSoft JSON无法处理字典键值和结构.

请检查我的代码:

public class CustomJSONSerializationHelper
{
    public string CustomSerialize(Dictionary<AuthorizationKey, ConditionalActionFlags> actionFlagMappings)
    {
        // ToArray() and use custom convertors, because NewtonSoft JSON can't handle dictionary key values and struct.
        var jsonString = JsonConvert.SerializeObject(actionFlagMappings.ToArray(), new Newtonsoft.Json.Converters.StringEnumConverter(), new AuthorizationKeyJsonConverter());
        return jsonString;
    }

    public Dictionary<AuthorizationKey, ConditionalActionFlags> CustomDeserialize(string jsonActionFlagMappings)
    {
        var resultArray = CustomDeserializeOverLoad(jsonActionFlagMappings);
        return (resultArray != null) ? resultArray.ToList().ToDictionary(pair => pair.Key, pair => pair.Value) : null;
    }

    public KeyValuePair<AuthorizationKey, ConditionalActionFlags>[] CustomDeserializeOverLoad(string jsonActionFlagMappings)
    {
        var result = JsonConvert.DeserializeObject<KeyValuePair<AuthorizationKey, ConditionalActionFlags>[]>(jsonActionFlagMappings,
             new Newtonsoft.Json.Converters.StringEnumConverter(), new AuthorizationKeyJsonConverter());
        return result;
    }
}

我这样称呼它:

    private string ObjectToJSON(Dictionary<AuthorizationKey, ConditionalActionFlags> actionFlagsMapping)
    {
        CustomJSONSerializationHelper customSerializationHelper = new CustomJSONSerializationHelper();
        return customSerializationHelper.CustomSerialize(actionFlagsMapping);
    }