elp
elp

Reputation: 870

Reading values from JSON without decimal points as float or double

There is a JSON array with hundreds of values. I want to parse an array which also got values with and without decimal points. As soon there's an value without a decimal point i get an error message.

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double

For example i am reading the following values:

99.06   //ok
4038.6  //ok
9448.8  //ok
3810    //error, since this will be interpreted as integer

This is the code i am working with currently:

double x = (double) jsonArray.get(7); 

The JSON got the following structure:

"array:"[[6 values here, 7th value is supposed to be a double value, x values here], [ same ], [ ... ], ...]

Upvotes: 2

Views: 1455

Answers (1)

Anshuman
Anshuman

Reputation: 841

Don't type cast the variable if you are not sure that its an Integer or Double. Always use Wrapper class. Try the following

 Double.parseDouble(a.get(7).toString());

Upvotes: 3

Related Questions