CaptainUnicorn
CaptainUnicorn

Reputation: 31

C# retrieve JSON from URL

I am creating a program that gets the steam market prices for various CS:GO items, and compares them to one another. I am having trouble getting the steam market JSON into my program. Market JSON Here is my code:

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(); ;
}

I am getting this error when instanciating WebClient():

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: (500) Internal Server Error.

Is this even a valid JSON? If not, is there an alternative? I heard backpack.tf has an api as well. Would this be better? Thanks

Upvotes: 2

Views: 1132

Answers (2)

Chet
Chet

Reputation: 3729

Looks like the URL is malformed. I.just tried http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=Shadow%20Case and it returned a good response.

Of particular note is the enclosed parenthesis in your URL.

Upvotes: 1

KiwiPiet
KiwiPiet

Reputation: 1334

Fixed code:

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(); ;
}

Upvotes: 0

Related Questions