如何从Json中获取c#桌面应用程序的数据
本文关键字:桌面 应用程序 数据 获取 Json | 更新日期: 2023-09-27 18:02:47
我试图使用Json从网站获取数据,我想这是获得该数据的最佳方式,因为我可以从以下类型中进行选择:
- json。
- csv。
- csv_d。
- tsv。
- iqy。
来源:http://stormspire.net/tradeskillmaster-website-desktop-application/14856-%5Bbeta%5D-tsm-web-apis.html
这是一个Json字符串示例:
{
"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"
}
}
来源:http://api.tradeskillmaster.com/sample_auction_data.json
我试着从菜单中创建一个类,粘贴作为特殊选项,但由于有9000多个项目,这是一个烂摊子。我也尝试了几个网站格式化或从Json字符串制作类,但它只是不起作用,如果我使用一个小样本它添加了很多类,这是我到目前为止得到的代码:
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);
}
编辑:哎呀,忘了加上这个问题。
我想获得所有项目的itemName和marketValue,并在dataGridView上显示它们,我只得到Json字符串,但我不能利用数据,尝试反序列化它,我认为这是错误的。
TL;DR:我怎样才能做类似的事情呢?
dataGridView.Add(jObj[i].itemName, jObj[i].marketValue);
首先安装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);
}