简体   繁体   English

序列化Dictionary时JsonConvert意外行为

[英]JsonConvert unexpected behavior when serialising Dictionary

Hell all! 地狱所有人! I have a Dictionary<string,Dictionary<CustomClass,string>> that i want to serialise. 我有要序列化的Dictionary<string,Dictionary<CustomClass,string>> The result I expect is something like: 我期望的结果是这样的:

{
  "key1":{
          {
            "CustomClassProperty1":"val1",
            "CustomClassProperty2":"val2",
            "CustomClassProperty3":"val3"
          }:"Final STR",
          {
            "CustomClassProperty1":"val10",
            "CustomClassProperty2":"val2",
            "CustomClassProperty3":"val35"
          }:"Final STR4",
          {
            "CustomClassProperty1":"val100",
            "CustomClassProperty2":"val25",
            "CustomClassProperty3":"val300"
          }:"Final STR8"
         },
  "key2":{
          {
            "CustomClassProperty1":"val4",
            "CustomClassProperty2":"val5",
            "CustomClassProperty3":"val6"
          }:"Final STR 2"
         },
  "key3":{
          {
            "CustomClassProperty1":"val1",
            "CustomClassProperty2":"val7",
            "CustomClassProperty3":"val5"
          }:"Final STR 3",
          {
            "CustomClassProperty1":"val10",
            "CustomClassProperty2":"val2",
            "CustomClassProperty3":"val35"
          }:"Final STR0",
          {
            "CustomClassProperty1":"val100",
            "CustomClassProperty2":"val25",
            "CustomClassProperty3":"val300"
          }:"Final STR10"
         }
}

But instead i'm getting 但是相反,我越来越

{
  "key1":{
          "MyProjectNamespace.CustomClass":"Final STR",
          "MyProjectNamespace.CustomClass":"Final STR4"
         },
  "key2":{
          "MyProjectNamespace.CustomClass":"Final STR 2"
         },
  "key3":{
          "MyProjectNamespace.CustomClass":"Final STR 3"
         }
}

Can anyone tell me how to make it right? 谁能告诉我如何做到正确? I dont want the "namespace.classname" but the properties... I`m using Newtonsoft.Json btw... tks a lot! 我不想要“ namespace.classname”,但属性...我正在使用Newtonsoft.Json,顺便说一句!

Your desired output isn't in JSON format . 您所需的输出不是JSON格式 The left hand side of the : is the name of the object property, and must be a string. 的左手边:是的对象属性的名称,必须是一个字符串。 To get the format you've listed, you'll need to use another serializer that supports your non-JSON format. 要获取列出的格式,您需要使用另一个支持非JSON格式的序列化器。

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

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