Problems with Json Serialize Dictionary

后端 未结 5 2460
自闭症患者
自闭症患者 2021-02-19 16:55

whenever i try to serialize the dictionary i get the exception:

System.ArgumentException: Type 
\'System.Collections.Generic.Dictionary`2[[Foo.DictionarySerializ         


        
5条回答
  •  孤独总比滥情好
    2021-02-19 17:46

    I know it's late, but maybe someone else can make use of it in the future. You can achieve what you need using LINQ:

    Dictionary data = new Dictionary();
    
    data.Add(TestEnum.A, 1);
    data.Add(TestEnum.B, 2);
    data.Add(TestEnum.C, 3);
    
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Dictionary dataToSerialize = data.Keys.ToDictionary(p => p.ToString(), p => data[p]);
    string dataSerialized = serializer.Serialize(dataToSerialize);
    

提交回复
热议问题