简体   繁体   English

C#| 从URL中抓取JSON无法将字符串转换为int

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

I have a piece of code, which should grab info from a URL. 我有一段代码,应该从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. 获取名为lowest_price的值,然后将其解析为变量,但JSON中有一个$符号,所以我不得不将其删除,之后我无法正确解析JSON。

My code: 我的代码:

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

JSON JSON

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

Error: 错误:

Argument 1: cannot convert from 'string' to 'int' 参数1:无法从'string'转换为'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. tokenPrice [“lowest_price”]是一个字符串,c#不会自动为您转换类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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