-1

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.

user7335295
  • 401
  • 2
  • 7
  • 31
  • Does this answer your question? [C# JSON Serialization of Dictionary into {key:value, ...} instead of {key:key, value:value, ...}](https://stackoverflow.com/questions/4861138/c-sharp-json-serialization-of-dictionary-into-keyvalue-instead-of-keyk) – Leon Bohmann Dec 03 '20 at 13:05
  • @LeonBohmann so using the DataContractJsonSerializer is the answer here? – user7335295 Dec 03 '20 at 13:08
  • could you provide a minimal example with your datastructure containing some entries? I could test it out for you. – Leon Bohmann Dec 03 '20 at 13:11
  • I fear providing some examples is too laborious as I am filling the dictionaries dynamically during runtime. – user7335295 Dec 03 '20 at 13:14
  • 1
    `{"translations": [ "xy": { "de-DE": "Kommando1", "en-US": "Command1" }, "ab": { "de-DE": "Kommando2", "en-US": "Command2" } ]}`is not a valid json ... [also it's working without key/value](https://dotnetfiddle.net/c6QCIA) ... moreover list is not necessary – Selvin Dec 03 '20 at 13:15
  • @Selvin how did you remove the "key" and "value" keywords? – user7335295 Dec 03 '20 at 13:17
  • I didn't .. `Newtonsoft.Json.JsonConvert.SerializeObject` works fine with Dictionary – Selvin Dec 03 '20 at 13:18
  • I posted an answer, including @Selvin recommendation.. – Leon Bohmann Dec 03 '20 at 13:26

1 Answers1

0

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"
  }
}
Leon Bohmann
  • 402
  • 1
  • 4
  • 16
  • I dont get it.. In my example the output still contains the "key" and "value" keywords.. – user7335295 Dec 03 '20 at 13:40
  • Could you try to remove the `JsonProperty`-Attribute? – Leon Bohmann Dec 03 '20 at 13:43
  • sure, I have tried it, but still the same output. I convert the dictionaries in to a JToken object if that helps.. – user7335295 Dec 03 '20 at 13:46
  • And I try converting it with data = Serializer.Serialize(new SerializedJsonObject(data)); – user7335295 Dec 03 '20 at 13:49
  • Dont do that. By doing that you loose the possibility of the JsonSerializer to reflect the original type as being a Dictionary. I guess you should try to serialize and deserialize the list directly. Let me know if that helps. – Leon Bohmann Dec 03 '20 at 13:50
  • I doesn't work really. The output string now looks like this : \"Translations\": {\r\n \"action1\": {\r\n \"de-DE\": \"Aktion1 durchführbar\"\r\n },\r\n \"action2\": {\r\n \"cz-CZ\": \"Zahajit vymen\",\r\n \"de-DE\": \"Aktion2 durchführen\",\r\n \"en-US\": \"Execute battery change\",\r\n \"fr-FR\": \"EXECUTER REMPLACEMENT DE ACTION2\"\r\n },\r\n \"action3\": {\r\n \"cz-CZ\": \"Vytvoa vycisteni\",\r\n \"de-DE\": \"Aktion3 generieren\",\r\n \"en-US\": \"Action3 creation\",\r\n \"fr-FR\": \"GENERER – user7335295 Dec 03 '20 at 14:31
  • That is the raw string output. You have to write that to a file. `\r\n` is the `NewLineDelimiter` which is treated in editors as a new line.. – Leon Bohmann Dec 03 '20 at 14:58
  • Did this work? If so, please accept the answer :) – Leon Bohmann Dec 09 '20 at 17:35