将RootObject属性转换为Double会导致崩溃

时间:2017-12-10 11:56:56

标签: c# xamarin

当我尝试将RootObject中的属性转换为Double时,会导致我的应用崩溃。为什么会发生这种情况,我该如何解决?

我的RootObject:

public class Rootobject
{
    public Double wantedDegree
    {
        get
        {
            return wantedDegree;
        }
        set
        {
            wantedDegree = Convert.ToDouble(value);
        }
    }
    public Double currentDegree
    {
        get
        {
            return currentDegree;
        }
        set
        {
            currentDegree = Convert.ToDouble(value);
        }
    }
}

调用RootObject:

var Items = JsonConvert.DeserializeObject<Rootobject>(content);
currentDegree.Text = String.Format("{0:F1}°", Items.currentDegree);
degreeControls.Value = Items.wantedDegree;

我的JSON数组(内容):

{
"wantedDegree": 22,
"currentDegree": 20
}

1 个答案:

答案 0 :(得分:2)

这应该做的工作:

public class RootObject
{
    [JsonProperty("wantedDegree")]
    public Double WantedDegree { get; set; }

    [JsonProperty("currentDegree")]
    public Double CurrentDegree { get; set; }
}

然后使用:

var Items = JsonConvert.DeserializeObject<Rootobject>(content);