简体   繁体   中英

JsonConvert unexpected behavior when serialising Dictionary

Hell all! I have a Dictionary<string,Dictionary<CustomClass,string>> that i want to serialise. 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!

Your desired output isn't in JSON format . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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