【问题标题】:C# Serialize dictionary without "key" and "value" keywords [duplicate]没有“key”和“value”关键字的C#序列化字典[重复]
【发布时间】:2020-12-03 12:54:22
【问题描述】:

我正在尝试将以下数据结构序列化为 JSON:

public class TestClass {

      public TestClass() {
         Translations = new List<Dictionary<string, Dictionary<string, string>>>();
      }

      [JsonProperty("translations")]
      public List<Dictionary<string, Dictionary<string, string>>> Translations { get; set; }
}

结果 json 字符串应如下所示:

„translations“: [
    „xy“: {
       „de-DE“: „Kommando1“,
       „en-US“: „Command1“,
       (…)
    },
    „ab“: {
       „de-DE“: „Kommando2“,
       „en-US“: „Command2“,
       (…)
    }

]

但是,这是当前的输出:

"translations": [
        [
          {
            "Key": "action1",
            "Value": [
              {
                "Key": "de-DE",
                "Value": "Aktion 1 durchgeführt"
              }
            ]
          }
        ],
        [
          {
            "Key": "Aktion2",
            "Value": [
              {
                "Key": "cz-CZ",
                "Value": "Zahajit vymenu "
              },
              {
                "Key": "de-DE",
                "Value": "Aktion2 durchführen"
              },
              {
                "Key": "en-US",
                "Value": "Execute action2"
              },
              {
                "Key": "fr-FR",
                "Value": "EXECUTER E Actione"
              }
            ]
          }
        ],
        [
          {
            "Key": "Action3",
            "Value": [
              {
                "Key": "cz-CZ",
                "Value": "Vytvorit na vycisteni"
              },
              {
                "Key": "de-DE",
                "Value": "Aktion3 generieren"
              },
              {
                "Key": "en-US",
                "Value": "Action3 creation"
              },
              {
                "Key": "fr-FR",
                "Value": "GENERER MISSION"
              }
            ]
          }
        ], (...)

我想知道如何在没有“key”和“value”字符串的情况下序列化字典,但直接使用值(如上面的示例):所以"en-US": "command2" 而不是"key": "en-US", "value": "command2"。如果使用字典无法做到这一点,那么我想知道使用哪个容器来实现这种格式。

【问题讨论】:

标签: c# json dictionary serialization json-serialization


【解决方案1】:

我现在正在写一个答案,因为我在没有任何必要工作的情况下让它工作:

我得到以下 json 输出:

[
  {
    "first": {
      "de-De": "test1",
      "de-De1": "test2",
      "de-De2": "test3"
    },
    "second": {
      "en-US": "test1us",
      "en-US1": "test2us",
      "en-US2": "test3us"
    }
  }
]

只需这样做:

var Translations = new List<Dictionary<string, Dictionary<string, string>>>();

var dic = new Dictionary<string, string>
{
    {"de-De","test1" },
    {"de-De1","test2" },
    {"de-De2","test3" },
};
    
var dic2 = new Dictionary<string, string>
{
    {"en-US","test1us" },
    {"en-US1","test2us" },
    {"en-US2","test3us" },
};

var maindic = new Dictionary<string, Dictionary<string, string>>();            
maindic.Add("first", dic);
maindic.Add("second", dic2);
    
Translations.Add(maindic);
    
var output = JsonConvert.SerializeObject(Translations, Formatting.Indented);

编辑:

正如@Selvin 提到的,您似乎不需要List&lt;...&gt;,因为列出的字典已经包含您要使用的键。所以我会选择:

var Translations = new Dictionary<string, Dictionary<string, string>>();

var dic = new Dictionary<string, string>
{
    {"action1","test1" },
    {"action2","test2" },
    {"action3","test3" },
};
    
var dic2 = new Dictionary<string, string>
{
    {"action1","test1us" },
    {"action2","test2us" },
    {"action3","test3us" },
};


Translations.Add("de-DE", dic);
Translations.Add("en-US", dic2);
    
var output = JsonConvert.SerializeObject(Translations, Formatting.Indented);

最终导致:

{
  "first": {
    "de-De": "test1",
    "de-De1": "test2",
    "de-De2": "test3"
  },
  "second": {
    "en-US": "test1us",
    "en-US1": "test2us",
    "en-US2": "test3us"
  }
}

【讨论】:

  • 我不明白.. 在我的示例中,输出仍然包含“key”和“value”关键字..
  • 您可以尝试删除JsonProperty-属性吗?
  • 当然,我试过了,但输出还是一样。如果有帮助,我将字典转换为 JToken 对象..
  • 我尝试用 data = Serializer.Serialize(new SerializedJsonObject(data)); 转换它
  • 不要那样做。通过这样做,您失去了 JsonSerializer 将原始类型反映为字典的可能性。我想你应该尝试直接序列化和反序列化列表。让我知道这是否有帮助。
猜你喜欢
  • 2015-05-15
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 2015-07-24
  • 2015-07-08
  • 1970-01-01
  • 2010-11-20
相关资源
最近更新 更多