0

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:

  • json.
  • csv.
  • csv_d.
  • tsv.
  • iqy.

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);
3
  • I want to use the data to fill a dataGridView, but I don't know how, tried deserializing it and making a list, but it seems empty. Commented Jul 15, 2015 at 16:27
  • the issue is that the json is not an array. you may need to make a custom JSON.Net converter to process the json to your liking. Commented Jul 15, 2015 at 16:28
  • Deserialize it as Dictionary<string, SomeObject> Commented Jul 15, 2015 at 16:39

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I must have a poster of you hanging in my room.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.