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
},
}
}
}
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.
This is what I got for now:
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;
}
}
}
With the code above I'm reaching the keyboard shortcut like "F10", "Ctrl+F1", but can't get the "Parent Name" (Newbie).
There is a easy way to read this kind of JSON structure?
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..
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With