简体   繁体   English

字典中的双精度值

[英]Double value in Dictionary

I have this code to get two double from jSon: 我有这段代码可以从jSon获得两个加倍:

Dictionary<string, object> dict = this.GetDicFromInput();
Decimal tmpLat = (Decimal)dict["lat"];
Decimal tmpLon = (Decimal)dict["lon"];

This is GetDicFromInput : 这是GetDicFromInput

private Dictionary<string, object> GetDicFromInput()
{
    string input = null;

    input = "{\"lon\":34.806109,\"lat\":31.9599733}";

    var json = new JavaScriptSerializer() { MaxJsonLength = int.MaxValue };
    Dictionary<string, object> dict = (Dictionary<string, object>)json.DeserializeObject(input);

    return dict;
}

And when I run it I get: 当我运行它时,我得到:

 [NullReferenceException: Object reference not set to an instance of an object.]
   BabySitter.Search.Page_Load(Object sender, EventArgs e) in \\psf\home\Documents\Visual Studio 2010\Projects\BabySitter\BabySitter\Search.aspx.cs:28
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +91
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

This is the fail line: 这是失败行:

Decimal tmpLat = (Decimal)dict["lat"];

Use the generic function to return the exact object type you are creating. 使用通用函数返回您要创建的确切对象类型。

public T Deserialize<T>(string input)

In this case, 在这种情况下,

Dictionary<string, object> dict = json.DeserializeObject<Dictionary<string, object>(input);

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

相关问题 清单 <double> 用于字典查找特定项的值 - List<double> for value of dictionary lookup specific item C#双字典更改值 - C# Double dictionary change value 我可以检查一个双值是否在字典内的对值之间<double,double></double,double> - Can i check if a double value is between pair values inside a Dictionary<double,double> 以double形式转换字典键 - Converting a dictionary key in double 尝试在Dictionary类型的a中更新值时防止双重哈希操作<IComparable, int> - Preventing double hash operation when trying to update value in a of type Dictionary<IComparable, int> 转换字典 <string , string[]> 到字典 <string , double[]> ? - Converting Dictionary <string , string[]> to Dictionary <string , double[]>? 字典“ContainsKey”上的双重检查锁定 - Double checked locking on Dictionary “ContainsKey” 将字符串转换为字典<string, double></string,> - Convert string into dictionary<string, double> DataContractSerializer反序列化枚举字典(和double) - DataContractSerializer DeSerialize Dictionary of enum (and double) 如何使用 Linq 将 JSON 文件中两个不同位置的数据转换为 C# 字典中的键(字符串)和值(双精度)? - How to convert data from two different locations in JSON file to a key(string) and value(double) within a C# dictionary, using Linq?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM