Fatima
Fatima

Reputation: 869

Why does GSON put a decimal point and a zero after all my JSON numbers which originally do not contain any decimal?

I read the JSON from a web service and converted all the JSON objects to HashMap<String, String> using GSON.

I tested and my JSONObject it's fine (no decimal point in any of the numbers), but the map object has the items with all numbers having a decimal point and a zero after that.

Here is my code:

try {
    jsonArray = json.getJSONArray("PropertyListings");
    for(int i = 0; i < jsonArray.length(); i++){
        JSONObject c = jsonArray.getJSONObject(i);
        HashMap<String, String> map = new HashMap<String, String>();
        map = (HashMap<String, String>) new Gson().fromJson(jsonArray.getString(i), map.getClass());
        listOfProperties.add(map);
    }

Upvotes: 5

Views: 2252

Answers (1)

MSpeed
MSpeed

Reputation: 8280

From Wikipedia. JSON's basic types are:

Number — a signed decimal number that may contain a fractional part and may use exponential E notation. JSON does not allow non-numbers like NaN, nor does it make any distinction between integer and floating-point. (Even though JavaScript uses a double-precision floating-point format for all its numeric values, other languages implementing JSON may encode numbers differently)

It makes no distinction between ints and floats. So it's thinking that every number may have a fractional part, so when you're converting a number to a String, it's leaving that part in. Try converting it to an Integer instead.

Upvotes: 1

Related Questions