2017-08-01 229 views
-2

嗨我有這個JSON文件(節選):我創建了一個不和諧BOT這是爲了響應命令和命令的一個是*卡(卡名稱) 此JSON文件提供 https://hastebin.com/uzifiteqol.json 但我希望用戶根據idname(卡片名稱)訪問lvl 9的統計數據(例如:卡片名稱) 例如: *卡片箭頭 這應該會給使用者區域傷害lvl 9箭頭顯示在JSON中,但我不確定如何訪問此屬性。繼承人是我到目前爲止有:從JSON文件C#閱讀

string jsonString = File.ReadAllText("/Users/me/Desktop/cardstatsfile.json"); 

這是JSON文件位置是我的電腦(MAC) 上,我不知道如何從那裏,我在想,我需要JToken.parse和通過文件解析來查找「idname」或用戶指定的卡。提前致謝!

編輯:澄清:因爲有75張牌,我想通過提供idname來獲得elixircost,稀有等等的值以及所有的lvl 9統計數據。或者我需要重新格式化JSON來執行此操作?

+1

看起來你應該寫一個模型類,和反序列化JSON的作爲類型的列表。在Web和Json.NET教程中有很多這樣的例子。 –

回答

1

編寫一些POCO類以對象形式反序列化後存儲該數據。使用Newtonsoft進行反序列化任務。上課是你的朋友。

3

反序列化的POCO下面,然後執行操作

public class Stat 
{ 
    public int level { get; set; } 
    public int areaDamage { get; set; } 
    public int crownTowerDamage { get; set; } 
    public int? hitpoints { get; set; } 
    public int? dps { get; set; } 
} 

public class RootObject 
{ 
    public string _id { get; set; } 
    public string idName { get; set; } 
    public string rarity { get; set; } 
    public string type { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public int arena { get; set; } 
    public int elixirCost { get; set; } 
    public int order { get; set; } 
    public int __v { get; set; } 
    public int radius { get; set; } 
    public string target { get; set; } 
    public List<Stat> stats { get; set; } 
    public string statsDate { get; set; } 
    public int? hitSpeed { get; set; } 
    public int? speed { get; set; } 
    public int? deployTime { get; set; } 
    public double? range { get; set; } 
    public int? count { get; set; } 
    public string transport { get; set; } 
}