Reputation: 343
Hi i have mention my json format below
[{
"id": "1",
"MinValue": 2,
"MaxValue": 29
}, {
"id": "2",
"MinValue": 0.5,
"MaxValue": 5.6
}]
While i am parsing the MinValue & MaxValue its return like 2.0,29.0(float) Kindly help me to get exact value.
My parsing code
JSONArray jsonArray = new JSONArray(result);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject profileObject = jsonArray.getJSONObject(i);
String minValue = profileObject.getString(Constants.VALUE_BMCPROFILE_MINVALUE);
String maxValue = profileObject.getString(Constants.VALUE_BMCPROFILE_MAXVALUE);
}
}
}
Upvotes: 4
Views: 529
Reputation: 8562
If you are really getting trouble then the alternate solution is remove .0
like this
if(MinValue.endsWith(".0")){
String myMinValue = MinValue.substring(0,MinValue.indexOf('.'))
}
Upvotes: 0
Reputation: 43334
Something like this should work
JSONArray jsonArray = new JSONArray(result);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
try {
JSONObject profileObject = jsonArray.getJSONObject(i);
Object minValue = profileObject.get(Constants.VALUE_BMCPROFILE_MINVALUE);
if (minValue instanceof Integer) {
int min = (int) minValue;
} else if (minValue instanceof Double) {
double min = (double) minValue;
}
Object maxValue = profileObject.get(Constants.VALUE_BMCPROFILE_MAXVALUE);
if (maxValue instanceof Integer) {
int max = (int) maxValue;
} else if (maxValue instanceof Double) {
double max = (double) maxValue;
}
} catch (Exception e) {
}
}
}
Then proceed with the int or double to do what you want
Upvotes: 0
Reputation: 60973
You can get the double value from JSON
then remove .0
if needed
DecimalFormat decimalFormat=new DecimalFormat("#.#");
double minValue = decimalFormat.format(profileObject.getDouble(Constants.VALUE_BMCPROFILE_MINVALUE));
double maxValue = decimalFormat.format(profileObject.getDouble(Constants.VALUE_BMCPROFILE_MAXVALUE));
Upvotes: 0
Reputation: 6165
Replace this with
String minValue = profileObject.getString(Constants.VALUE_BMCPROFILE_MINVALUE);
String maxValue = profileObject.getString(Constants.VALUE_BMCPROFILE_MAXVALUE);
this
double minValue = profileObject.getDouble(Constants.VALUE_BMCPROFILE_MINVALUE, 0);
double maxValue = profileObject.getDouble(Constants.VALUE_BMCPROFILE_MAXVALUE, 0);
as per json data min and max values are clearly Double not String.
Upvotes: 1