【发布时间】: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"。如果使用字典无法做到这一点,那么我想知道使用哪个容器来实现这种格式。
【问题讨论】:
-
@LeonBohmann 所以使用 DataContractJsonSerializer 是这里的答案?
-
您能否提供一个包含一些条目的数据结构的最小示例?我可以帮你测试一下。
-
我担心提供一些示例太费力了,因为我在运行时动态填充字典。
-
{"translations": [ "xy": { "de-DE": "Kommando1", "en-US": "Command1" }, "ab": { "de-DE": "Kommando2", "en-US": "Command2" } ]}不是一个有效的 json ... also it's working without key/value ... 而且列表不是必需的
标签: c# json dictionary serialization json-serialization