2017-08-01 178 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; } 
}