简体   繁体   中英

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'

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

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("$", ""));

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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM