C#从URL检索JSON

时间:2015-11-09 23:33:46

标签: c# json steam steam-web-api

我正在创建一个程序来获取各种CS:GO项目的蒸汽市场价格,并将它们相互比较。我无法将蒸汽市场JSON纳入我的计划。 Market JSON 这是我的代码:

using (WebClient webClient = new System.Net.WebClient())
{
    WebClient n = new WebClient(); // <-- error on this line
    string json = n.DownloadString("http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P250%20%7C%20Valence%20(Factory%20New");
    dynamic array = JsonConvert.DeserializeObject(json);
    test.Text = array.lowest_price.ToString(); ;
}

我在发送WebClient()时遇到此错误:

  

未处理的类型&#39; System.Net.WebException&#39;发生在   System.dll中

     

其他信息:远程服务器返回错误:(500)   内部服务器错误。

这甚至是有效的JSON吗?如果没有,有替代方案吗?我听说backpack.tf也有api。这会更好吗? 感谢

2 个答案:

答案 0 :(得分:1)

看起来网址格式不正确。 I.just尝试http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Shadow%20Case并且它得到了很好的回应。

特别需要注意的是网址中的括号。

答案 1 :(得分:0)

固定代码:

using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString("http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=P250%20%7C%20Valence%20(Factory%20New)");
    dynamic array = JsonConvert.DeserializeObject(json);
    var price = array.lowest_price.ToString(); ;
}