I'm trying to parse some JSON using JSON.NET but whenever I try to get the value from any of the values, it returns 0. I just want to get the values from the first set of data. Here is my code:
string text = listBox1.SelectedItem.ToString();
text = text.Substring(text.LastIndexOf(": ") + 2);
string url = "http://www.gw2spidy.com/api/v0.9/json/item-search/" + text + "/1";
var json = new WebClient().DownloadString(url);
Result itemPrices = JsonConvert.DeserializeObject<Result>(json);
int buyPrice = itemPrices.min_sale_unit_price;
int sellPrice = itemPrices.max_offer_unit_price;
sellPriceLabel.Content = "Highest Sell Price: " + sellPrice;
buyPriceLabel.Content = "Lowest Buy Price: " + buyPrice;
And here is my JSON objects class:
public class Result
{
public int data_id { get; set; }
public string name { get; set; }
public int rarity { get; set; }
public int restriction_level { get; set; }
public string img { get; set; }
public int type_id { get; set; }
public int sub_type_id { get; set; }
public string price_last_changed { get; set; }
public int max_offer_unit_price { get; set; }
public int min_sale_unit_price { get; set; }
public int offer_availability { get; set; }
public int sale_availability { get; set; }
public int sale_price_change_last_hour { get; set; }
public int offer_price_change_last_hour { get; set; }
}
public class RootObject
{
public int count { get; set; }
public int page { get; set; }
public int last_page { get; set; }
public int total { get; set; }
public List<Result> results { get; set; }
}
Here is the JSON I am trying to parse:
{
"count": 3,
"page": 1,
"last_page": 1,
"total": 3,
"results": [
{
"data_id": 12223,
"name": "Apple Pie",
"rarity": 2,
"restriction_level": 10,
"img": "https://render.guildwars2.com/file/0A50099C343F01AC2846ADF4C8A948BA76F4DBC1/63097.png",
"type_id": 3,
"sub_type_id": 1,
"price_last_changed": "2015-05-05 20:58:24 UTC",
"max_offer_unit_price": 136,
"min_sale_unit_price": 226,
"offer_availability": 22161,
"sale_availability": 4007,
"sale_price_change_last_hour": 0,
"offer_price_change_last_hour": 0
},
{
"data_id": 12150,
"name": "Eda's Apple Pie",
"rarity": 1,
"restriction_level": 5,
"img": "https://render.guildwars2.com/file/13380176D1D569B5DD077F7DD8C412CAE9E77527/63254.png",
"type_id": 3,
"sub_type_id": 1,
"price_last_changed": "2015-05-05 23:31:06 UTC",
"max_offer_unit_price": 160,
"min_sale_unit_price": 313,
"offer_availability": 3596,
"sale_availability": 2744,
"sale_price_change_last_hour": 0,
"offer_price_change_last_hour": 0
},
{
"data_id": 9497,
"name": "Eda's Apple Pie Recipe",
"rarity": 1,
"restriction_level": 0,
"img": "https://render.guildwars2.com/file/B7B167286DD34B254E22682900C6EF2310F6EE0E/849342.png",
"type_id": 3,
"sub_type_id": 6,
"price_last_changed": "2014-09-11 10:12:00 UTC",
"max_offer_unit_price": 10101,
"min_sale_unit_price": 0,
"offer_availability": 0,
"sale_availability": 0,
"sale_price_change_last_hour": 0,
"offer_price_change_last_hour": 0
}
]}
DeserializeObject<Result>should beDeserializeObject<RootObject>RootObject itemPrices = JsonConvert.DeserializeObject<RootObject>(json);. And then later on you access members ofResultwhen really you should be accessing members ofRootObject. You're trying to accessResults directly, but that's now how the JSON you posted is structured.