Unable to cast object of type `System.Int64` to type `System.String`.

huangapple go评论80阅读模式
英文:

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&lt;string, object&gt; featureProperties = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(properties);
            
 gJFeature.id =Convert.ToString(featureProperties[&quot;OBJECTID&quot;]);

答案3

得分: 0

看起来你的 object 是装箱的 Int64 (long)
你不能将 long 直接转换为 string。你需要通过 ToString() 进行转换。

gJFeature.id = int.Parse(featureProperties[&quot;OBJECTID&quot;].ToString());

或者如果你可以首先取消装箱你的 long,然后将其转换为 int

gJFeature.id = (int)(long)featureProperties[&quot;OBJECTID&quot;];

英文:

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[&quot;OBJECTID&quot;].ToString());

Or if you can first unbox your long and then cast it to int:

gJFeature.id = (int)(long)featureProperties[&quot;OBJECTID&quot;];

huangapple
  • 本文由 发表于 2023年1月9日 14:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定