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.
I am writing an answer now because I got it working without any necessary work:
I get the following json-output:
By simply doing this:
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:which ultimately leads to: