我在一个JSON文件中存储了一组浮点数,以下是JSON列表的样式:
"RollSize": "[17.5,18.0,19.0,23.5,26.5,35.0,35.5,38.0]"
我使用的是一个返回对象列表的方法,因为有多个列表需要返回。然后我将对象列表转换成浮点数。但是当这样做时,会收到一个“指定的转换无效”异常。但是如果我将对象列表转换为双精度,则可以正常工作。以下是两种方法:
private void DisplayCutOffs(object sender, EventArgs e) {
try {
// Errors here unless I cast to double
_view.CurrentCutOffValues = _systemVariablesManager.ReturnListBoxValues("CutOff").Cast<float>().ToList();
}
catch (Exception ex) {
LogErrorToView(this, new ErrorEventArgs(ex.Message));
}
}
存储库方法:
public List<object> ReturnListBoxValues(string propertyName) {
if (File.Exists(expectedFilePath)) {
var currentJsonInFile = JObject.Parse(File.ReadAllText(expectedFilePath));
return JsonConvert.DeserializeObject<List<object>>(currentJsonInFile["SystemVariables"][propertyName].ToString());
}
else {
throw new Exception("Setup file not located. Please run the Inital Set up application. Please ask Andrew for more information.");
}
}
然而我注意到,如果我在foreach循环中循环列表,我可以将每个值转换为浮点数。所以我不确定这里发生了什么。
有人知道吗?