gpt4 book ai didi

c# - 使用 "random"键反序列化 JSON

转载 作者:行者123 更新时间:2023-12-02 21:00:42 27 4
gpt4 key购买 nike

我正在尝试反序列化此 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
},
}
}
}

我在尝试读取它们的属性和值时遇到问题,但我无法获取名称属性,例如“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;
}
}
}

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

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

最佳答案

您可以使用 Newtonsoft。大多数情况下,我更喜欢解析 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; }
}

然后你可以这样读:

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..
}
}

关于c# - 使用 "random"键反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com