2017-06-01 76 views
1

正如它在標題im中試圖通過JObject循環獲取所有內部鍵名稱所描述的那樣,但是您可以看到這是在子鍵「Bayonet」下指定的。雖然這工作正常,我需要一個更通用的方法,因爲「Bayonet」子鍵不會總是存在。下面是一些我的嘗試:通過JObject循環以獲得屬性名稱

工作解決方案,但ungeneralized:

foreach (JProperty condition in SkinData[Skin]["Bayonet"]) 
{ 
    conditionlines.Add(condition.Name); 
} 

與失敗不能JObject轉換爲JProperty錯誤:

foreach (JProperty condition in SkinData[Skin].First) 
{ 
    conditionlines.Add(condition.Name); 
} 

沒有名字的字符串錯誤而失敗:

foreach (JProperty condition in SkinData[Skin][0]) 
{ 
    conditionlines.Add(condition.Name); 
} 

JSON:

{ 
    "Scorched":{ 
     "Bayonet":{ 
     "FN":"51", 
     "MW":"32", 
     "FT":"25", 
     "WW":"26", 
     "BS":"26" 
     }, 
     "Bowie Knife":{ 
     "MW":"25", 
     "FT":"20", 
     "WW":"21", 
     "FN":"41", 
     "BS":"20" 
     }, 
     "Butterfly Knife":{ 
     "FN":"50", 
     "MW":"36", 
     "FT":"29", 
     "WW":"28", 
     "BS":"29" 
     }, 
     "Falchion Knife":{ 
     "FN":"31", 
     "MW":"20", 
     "FT":"17", 
     "WW":"17", 
     "BS":"17" 
     }, 
     "Flip Knife":{ 
     "FN":"0", 
     "MW":"24", 
     "FT":"20", 
     "WW":"20", 
     "BS":"20" 
     }, 
     "Gut Knife":{ 
     "FN":"0", 
     "MW":"19", 
     "FT":"17", 
     "WW":"18", 
     "BS":"17" 
     }, 
     "Huntsman Knife":{ 
     "FN":"48", 
     "MW":"27", 
     "FT":"21", 
     "WW":"21", 
     "BS":"20" 
     }, 
     "Karambit":{ 
     "FN":"87", 
     "MW":"55", 
     "FT":"43", 
     "WW":"42", 
     "BS":"41" 
     }, 
     "M9 Bayonet":{ 
     "FN":"54", 
     "MW":"38", 
     "FT":"30", 
     "WW":"30", 
     "BS":"30" 
     }, 
     "Shadow Daggers":{ 
     "FN":"29", 
     "MW":"20", 
     "FT":"17", 
     "WW":"17", 
     "BS":"17" 
     } 
    } 
} 
+0

你可能想看看我是怎麼做類似的[這裏](https://stackoverflow.com/a/44267151/3932049) –

+0

@CamiloTerevinto那麼奇怪的事情是爲什麼它與一個合格的鍵,但不與。首先,那就是我不懂的 – Nikolas

回答

1

你可以這樣做:

foreach (var condition in ((JContainer)SkinData[Skin]).Descendants().OfType<JObject>().Take(1).Properties()) 
{ 
    conditionlines.Add(condition.Name); 
}  

此發現的第一個descendant(如有的話)SkinJObject類型指定令牌,然後遍歷其properties

樣本fiddle

+0

太棒了,謝謝! – Nikolas