英文:
C# Unable to cast object of type system int64 to type system string
问题
gJFeature.id = int.Parse((string)featureProperties["OBJECTID"]);
**id= integer type**
> 无法将对象转换为整数。
有人可以帮我找到解决方案吗?
英文:
Dictionary<string, object> featureProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(properties);
gJFeature.id = int.Parse((string)featureProperties["OBJECTID"]);
id= integer type
> Unable to convert object to interger.
Can any one help me find out the solution.
答案1
得分: 1
我们可以得出结论,featureProperties["OBJECTID"]
不是一个 string
,而是一个 long
(又称 Int64
)。
考虑改用:
gJFeature.id = Convert.ToInt32(featureProperties["OBJECTID"]);
它可以处理广泛的可能值。
英文:
We can conclude that featureProperties["OBJECTID"]
is not a string
, but a long
(aka Int64
).
Consider instead:
gJFeature.id = Convert.ToInt32(featureProperties["OBJECTID"]);
which handles a broad range of possible values.
答案2
得分: 0
请看以下翻译:
// 做类似这样的事情:
Dictionary<string, object> featureProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(properties);
gJFeature.id = Convert.ToString(featureProperties["OBJECTID"]);
英文:
Do something like this :
Dictionary<string, object> featureProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(properties);
gJFeature.id =Convert.ToString(featureProperties["OBJECTID"]);
答案3
得分: 0
看起来你的 object
是装箱的 Int64 (long)
。
你不能将 long
直接转换为 string
。你需要通过 ToString()
进行转换。
gJFeature.id = int.Parse(featureProperties["OBJECTID"].ToString());
或者如果你可以首先取消装箱你的 long
,然后将其转换为 int
:
gJFeature.id = (int)(long)featureProperties["OBJECTID"];
英文:
Looks like your object
is boxed Int64 (long)
.
You cannot cast long
to string
. You need to convert it via ToString()
.
gJFeature.id = int.Parse(featureProperties["OBJECTID"].ToString());
Or if you can first unbox your long
and then cast it to int
:
gJFeature.id = (int)(long)featureProperties["OBJECTID"];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论