Home > Net >  C# Unable to cast object of type system int64 to type system string
C# Unable to cast object of type system int64 to type system string

Time:01-09

 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.

CodePudding user response:

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.

CodePudding user response:

Do something like this :

 Dictionary<string, object> featureProperties = JsonConvert.DeserializeObject<Dictionary<string, object>>(properties);
            
 gJFeature.id =Convert.ToString(featureProperties["OBJECTID"]);

CodePudding user response:

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"];

  •  Tags:  
  • c#
  • Related