使用“随机"反序列化JSON钥匙 [英] Deserialize JSON with "random" key
问题描述
我正在尝试反序列化此Json代码:
I'm trying to Deserialize this Json code:
"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
},
}
}
}
我在尝试读取它们的属性和值时遇到问题,但是我无法获得Name属性,例如"Newbie","Mega Mage","Paladin"等.
I'm having problems trying to read their properties and values, but I can't get the Name property like the "Newbie", "Mega Mage", "Paladin", etc.
这是我现在得到的:
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",但无法获得父母姓名"(新手).
With the code above I'm reaching the keyboard shortcut like "F10", "Ctrl+F1", but can't get the "Parent Name" (Newbie).
有一种简单的方法可以读取这种JSON结构吗?
There is a easy way to read this kind of JSON structure?
推荐答案
您可以使用Newtonsoft.大多数情况下,我更喜欢将json解析为类.解决问题的示例:
You can use Newtonsoft. And mostly i prefer parsing json to classes. Example for solving your problem:
首先定义反序列化的类:
First define classes for deserialize:
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; }
}
然后您可以像这样阅读它:
Then you can read it like this:
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..
}
}
这篇关于使用“随机"反序列化JSON钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!