0

Hey guys I have this json file (excerpt): https://hastebin.com/uzifiteqol.json I have created a discord bot which responds to commands and one of the commands is *card (card name) This JSON file provides the stats of all the cards, but I want the user to access the lvl 9 stats based on the idname (card name he puts) For example: *card arrows This should give the user the area damage of lvl 9 arrows that are shown in the json, but I am not sure how to access this stat. Heres what I have so far:

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

This is where the json file location is on my computer (mac) I am not sure how to go from there, I was thinking that I would need JToken.parse and parse through the file to look for the "idname" or the card specified by the user. Thanks in advance!

EDIT: Clarification: Because there are 75 cards, I want to obtain the values of elixircost, rarity, ect and also all the lvl 9 stats just by providing the idname. Or do I need to reformat the JSON to do this?

  • 1
    It looks like you should probably write a model class, and deserialize the JSON as a list of that type. There are *lots* of examples of this on the web and in Json.NET tutorials. – Jon Skeet Aug 01 '17 at 06:08

2 Answers2

4

Deserialize to a POCO as below and then do the operations

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; }
}
Ansil F
  • 284
  • 1
  • 8
1

Write some POCO class to store that data after deserialization in object form. Use Newtonsoft for the deserialization task. JsonConvert class is your friend.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32