이 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"등과 같은 Name 속성을 가져올 수 없습니다.
이것이 내가 지금 얻은 것입니다.
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"과 같은 바로 가기 키에 도달했지만 "부모 이름"(Newbie)을 가져올 수 없습니다.
이런 종류의 JSON 구조를 쉽게 읽을 수있는 방법이 있습니까?
Newtonsoft를 사용할 수 있습니다. 그리고 대부분 나는 클래스보다 json을 파싱하는 것을 선호합니다. 문제 해결을위한 예 :
먼저 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; }
}
그런 다음 다음과 같이 읽을 수 있습니다.
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..
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다