使用“随机”键反序列化 JSON

Kyo*_*ore 3 c# json json.net

我正在尝试反序列化此 Json 代码:

"hotkeyOptions": {
        "autoSwitchHotkeyPreset": true,
        "currentHotkeySetName": "Paladin",
        "hotkeySets": {
            "Newbie": {
                "F10": {
                    "useObject": 5645,
                    "useType": "SelectUseTarget"
                },
                "F11": {
                    "useObject": 5456,
                    "useType": "SelectUseTarget"
                },
                "F12": {
                    "useObject": 7565,
                    "useType": "Use"
                },
                "F8": {
                    "useObject": 7547,
                    "useType": "UseOnYourself"
                },
                "F9": {
                    "useObject": 4214,
                    "useType": "SelectUseTarget"
                }
            },
            "Mega Mage": {
                "Ctrl+F1": {
                    "chatText": "heal friend",
                    "sendAutomatically": true
                },
                "Ctrl+F4": {
                    "chatText": "mega haste",
                    "sendAutomatically": true
                },
                "F1": {
                    "chatText": "haste",
                    "sendAutomatically": true
                },
                "F10": {
                    "useObject": 3412,
                    "useType": "SelectUseTarget"
                },
                "F11": {
                    "useObject": 5343,
                    "useType": "SelectUseTarget"
                },
            },
            "Paladin": {
                "F1": {
                    "useObject": 4643,
                    "useType": "UseOnYourself"
                },
                "F2": {
                    "useObject": 6433,
                    "useType": "UseOnYourself"
                },
                "F3": {
                    "chatText": "haste",
                    "sendAutomatically": true
                },
                "F5": {
                    "chatText": "heal",
                    "sendAutomatically": true
                }
            },
            "Mage": {
                "F1": {
                    "chatText": "explosion",
                    "sendAutomatically": true
                },
                "F12": {
                    "useObject": 3003,
                    "useType": "SelectUseTarget"
                }
            },
            "Knight": {
                "Ctrl+F1": {
                    "chatText": "poke go",
                    "sendAutomatically": true
                },
                "F1": {
                    "chatText": "haste",
                    "sendAutomatically": true
                },
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在尝试读取它们的属性和值时遇到问题,但我无法获取“Newbie”、“Mega Mage”、“Paladin”等名称属性。

这就是我现在得到的:

JToken token = JObject.Parse(json);
JToken hotkeyConfig = token.SelectToken("hotkeyOptions");

JToken activeHotkey = hotkeyConfig.SelectToken("currentHotkeySetName");
this.ActiveHotkeySet = activeHotkey.Value<string>(); //This is working, returning the "Paladin" string

JToken hotkeysSet = hotkeyConfig.SelectToken("hotkeySets");
foreach (var set in hotkeysSet.Children()) {
    foreach (JObject obj in set.Children<JObject>()) {
        foreach(JProperty prop in obj.Properties()) {
            var teste = prop.Name;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

通过上面的代码,我可以使用“F10”、“Ctrl+F1”等键盘快捷键,但无法获取“家长姓名”(新手)。

有没有一种简单的方法来读取这种JSON结构?

小智 5

您可以使用牛顿软件。大多数情况下,我更喜欢解析 json 而不是类。解决您的问题的示例:

首先定义反序列化的类:

public class Hotkeys
{
    [JsonProperty("hotkeyOptions")]
    public HotkeyOptions HotkeyOptions { get; set; }
}

public class HotkeyOptions
{
    [JsonProperty("autoSwitchHotkeyPreset")]
    public bool AutoSwitchHotkeyPreset { get; set; }

    [JsonProperty("currentHotkeySetName")]
    public string CurrentHotkeySetName { get; set; }

    [JsonProperty("hotkeySets")]
    public Dictionary<string, JObject> HotkeySets { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样读:

var hotkeys = JsonConvert.DeserializeObject<Hotkeys>(json);

foreach(var hotkeySet in hotkeys.HotkeyOptions.HotkeySets)
{
    string hotkeySetName = hotkeySet.Key; // "Newbie" etc..
    foreach(var hotkey in hotkeySet.Value)
    {
        string hotkeyString = hotkey.Key; // "F10" etc..
    }
}
Run Code Online (Sandbox Code Playgroud)