How to obtain data from Json for C# desktop app

I'm trying to use Json to obtain data from a website, I suppose it's the best way to obtain that data since I can choose from this types:

Source: http://stormspire.net/tradeskillmaster-website-desktop-application/14856-%5Bbeta%5D-tsm-web-apis.html

This is a sample Json string:

{  
   "25":{  
      "itemName":"Worn Shortsword",
      "marketValue":"120000000",
      "minBuyout":"0",
      "historicalPrice":"0",
      "quantity":"0",
      "globalMarketValue":"27679191",
      "globalMinBuyout":"23303673",
      "globalHistoricalPrice":"13771960",
      "globalQuantity":"1",
      "globalSalePrice":"0"
   },
   "35":{  
      "itemName":"Bent Staff",
      "marketValue":"10000000",
      "minBuyout":"0",
      "historicalPrice":"6670500",
      "quantity":"0",
      "globalMarketValue":"15850430",
      "globalMinBuyout":"11381812",
      "globalHistoricalPrice":"4527059",
      "globalQuantity":"1",
      "globalSalePrice":"2061488"
   },
   "36":{  
      "itemName":"Worn Mace",
      "marketValue":"0",
      "minBuyout":"0",
      "historicalPrice":"0",
      "quantity":"0",
      "globalMarketValue":"827553",
      "globalMinBuyout":"1024444",
      "globalHistoricalPrice":"1903356",
      "globalQuantity":"1",
      "globalSalePrice":"10000"
   }
}

Source: http://api.tradeskillmaster.com/sample_auction_data.json

I've tried creating a class with the paste as special option from the menu, but as there are over 9000 items it's a mess. I've also tried several websites to format or make classes from the Json string, but it just doesn't work, and if I use a small sample it adds a lot of classes, this is the code I've got so far:

string url = "http://api.tradeskillmaster.com/sample_auction_data.json";
using (var w = new WebClient()) {
                var json_data = string.Empty;
                // attempt to download JSON data as a string
                try {
                    json_data = w.DownloadString(url);
                } catch (Exception error) {
                    MessageBox.Show(error.Message);
                }
                var jObj = JsonConvert.DeserializeObject<Item>(json_data);
}

EDIT: Oops forgot to add the problem.

I want to get the itemName and marketValue of all the items, and display them on a dataGridView, I only get the Json string, but I can't make use of the data, tried deserializing it I think it's wrong.

TL;DR: How can I make it so I can do something similar to this:

dataGridView.Add(jObj[i].itemName, jObj[i].marketValue);

Upvotes: 0

Views: 2050

Answers (1)

Nguyen Kien
Nguyen Kien

Reputation: 1927

First install Json.Net.

public class Item
{
    public string itemName { get; set; }
    public string marketValue { get; set; }
    public string minBuyout { get; set; }
    public string historicalPrice { get; set; }
    public string quantity { get; set; }
    public string globalMarketValue { get; set; }
    public string globalMinBuyout { get; set; }
    public string globalHistoricalPrice { get; set; }
    public string globalQuantity { get; set; }
    public string globalSalePrice { get; set; }
}


var result = JsonConvert.Deserialize<Dictionary<string, Item>>("json string")

foreach (var item in result)
{
    // do what you want with result
    Debug.WriteLine(item.Key);
}

Upvotes: 2

Related Questions