C# Serialize dictionary without "key" and "value" keywords

1.3k Views Asked by At

I am trying to serialize the following data structure to 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; }
}

The result json string should look like this:

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

]

But instead, this is the output currently:

"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"
              }
            ]
          }
        ], (...)

I would like to know how to serialize the dictionaries without the "key" and "value" strings, but with the values directly (like in the example above): So "en-US": "command2" instead of "key": "en-US", "value": "command2". If this is not possible using dictionary, then I would like to know which container to use in order to achieve this format.

1

There are 1 best solutions below

8
On

I am writing an answer now because I got it working without any necessary work:

I get the following json-output:

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

By simply doing this:

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);

EDIT:

As @Selvin mentioned, it seems like you do not need the List<...> since the listed dictionaries already contain the keys you want to use. So I would go with:

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);

which ultimately leads to:

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