无法从JTOken获得价值

时间:2022-02-06 20:15:51

I have this JToken

我有这个JToken

var pv = JToken.Parse(keys["parameterValues"].ToString()).ToList()[0];

Which returns this value

返回此值

{"DE:Actual Savings": 42217.0}

I can't use .Value because the float is represented as an object {42217.0}

我不能使用.Value,因为float表示为一个对象{42217.0}

How can I get this number? Right now I am using .ToString() and converting it

我怎么能得到这个号码?现在我正在使用.ToString()并转换它

1 个解决方案

#1


1  

To convert the value to a defined type you could use one of the defined methods below:

要将值转换为已定义的类型,您可以使用以下定义的方法之一:

System.Convert.ChangeType(jtoken.ToString(), targetType);

or

JsonConvert.DeserializeObject(jtoken.ToString(), targetType);

Let's take into consideration our sample:

让我们考虑一下我们的样本:

string json = @{"DE:Actual Savings": 42217.0}

You could do something like:

你可以这样做:

var obj = (JObject)JsonConvert.DeserializeObject(json);
Type type = typeof(float);
var i1 = System.Convert.ChangeType(obj["DE:Actual Savings"].ToString(), type);

Hope this solves your problem.

希望这能解决你的问题。

#1


1  

To convert the value to a defined type you could use one of the defined methods below:

要将值转换为已定义的类型,您可以使用以下定义的方法之一:

System.Convert.ChangeType(jtoken.ToString(), targetType);

or

JsonConvert.DeserializeObject(jtoken.ToString(), targetType);

Let's take into consideration our sample:

让我们考虑一下我们的样本:

string json = @{"DE:Actual Savings": 42217.0}

You could do something like:

你可以这样做:

var obj = (JObject)JsonConvert.DeserializeObject(json);
Type type = typeof(float);
var i1 = System.Convert.ChangeType(obj["DE:Actual Savings"].ToString(), type);

Hope this solves your problem.

希望这能解决你的问题。