James Aniszt
James Aniszt

Reputation: 69

C# | Grabbing JSON from URL cannot convert string to int

I have a piece of code, which should grab info from a URL. Get the value called lowest_price and then parse it into a variable, but there is a $ sign in the JSON so I had to remove it and after that I can't Parse the JSON correctly.

My code:

var tokenPrice = JObject.Parse(steamMarket).ToString().Replace("$", " ");
double marketPrice = tokenPrice["lowest_price"];

JSON

{"success":true,"lowest_price":"$5.61","volume":"6","median_price":"$5.61"}

Error:

Argument 1: cannot convert from 'string' to 'int'

Upvotes: 1

Views: 455

Answers (3)

Tim
Tim

Reputation: 613

tokenPrice["lowest_price"] is a string and c# will not automatically convert types for you.

var tokenPrice = JObject.Parse(steamMarket);
double marketPrice = double.Parse(tokenPrice["lowest_price"].ToString().Replace("$", ""));

Upvotes: 0

Palle Due
Palle Due

Reputation: 6292

You can also do:

string json = "{\"success\":true,\"lowest_price\":\"$5.61\",\"volume\":\"6\",\"median_price\":\"$5.61\"}";
var jObject = Newtonsoft.Json.Linq.JObject.Parse(json);
double tokenPrice = double.Parse((string )jObject["lowest_price"], NumberStyles.Currency, new CultureInfo("en-US"));

Upvotes: 0

Chris
Chris

Reputation: 729

double marketPrice = double.Parse(JObject.Parse(steamMarket)["lowest_price"].ToString().Replace("$", ""));

Upvotes: 3

Related Questions