Syed Kamran Ahmed
Syed Kamran Ahmed

Reputation: 107

Json to Double type JAVA

I am trying to get data from json to variables.One of variable is type of double but its getting 0.0 values.I tried some solution but they did'nt work.

This is my json: {"unitfactor":"0.1","unit":"N","canvassize":{"height":"302","width":"412"}

Code:

final JSONObject jObject = new JSONObject(fbeInput);
double mUnitFactor = jObject.getDouble("unitfactor");
String unit = jObject.getString("unit");

But mUnitFactor always gets 0.0 value. Even if i try to fetch unitfactor as string it did'nt show any value during debugging.

String mUnitFactor = jObject.getString("unitfactor");

Upvotes: 0

Views: 7227

Answers (3)

msagala25
msagala25

Reputation: 1806

"{"unitfactor":"0.1","unit":"N"}"

its because your unitfactor 0.1 is String in the making. change it to this:

"{"unitfactor":0.1,"unit":"N"}"

Remove the double quotation mark on 0.1.

or try to use the approach of @iNan:

double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor")); 

In this approach, you will first get the String unitfactor value, which is 0.1, and then you will parse it to Double using its wrapper class.

Upvotes: 2

Raghav
Raghav

Reputation: 4638

Since unitfactor is of type string, If you cannot change JSON then you can use

double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor"))

Upvotes: 1

Raut Darpan
Raut Darpan

Reputation: 1530

There is no unit for varaibles in json. for string use "" , double inverted and for numeric like double,float, long directly assign value

in your case:

{"unitfactor": 0.1, "unit": "N"}

Upvotes: 0

Related Questions