Search code examples
javajsondeserializationjson-deserialization

How to deserialize a JSON with integer as key?


I have a JSON which looks like this:

{
"MeterRates": {
                "0": 0.142,
                "1024": 0.142,
                "51200": 0.142,
                "512000": 0.142,
                "1024000": 0.1278,
                "5120000": 0.1051
            }
}

This JSON is actually a part of a larger JSON file, I extracted only the part I was having difficulty deserializing. I need to deserialize this into a Java object. I tried doing this using the following class, but it gives me null values for all keys.

public class MeterRates {
private Double rate0;
private Double rate1024;
private Double rate51200;
private Double rate512000;
private Double rate1024000;
private Double rate5120000;

@JsonProperty("0")
public Double getRate0() {
    return rate0;
}

public void setRate0(Double rate0) {
    this.rate0 = rate0;
}

@JsonProperty("1024")
public Double getRate1024() {
    return rate1024;
}

public void setRate1024(Double rate1024) {
    this.rate1024 = rate1024;
}

@JsonProperty("51200")
public Double getRate51200() {
    return rate51200;
}

public void setRate51200(Double rate51200) {
    this.rate51200 = rate51200;
}

@JsonProperty("512000")
public Double getRate512000() {
    return rate512000;
}

public void setRate512000(Double rate512000) {
    this.rate512000 = rate512000;
}

@JsonProperty("1024000")
public Double getRate1024000() {
    return rate1024000;
}

public void setRate1024000(Double rate1024000) {
    this.rate1024000 = rate1024000;
}

@JsonProperty("5120000")
public Double getRate5120000() {
    return rate5120000;
}

public void setRate5120000(Double rate5120000) {
    this.rate5120000 = rate5120000;
}

@Override
public String toString() {
    return "MeterRates [0 = " + rate0 + " 1024 = " + rate1024 + " 51200 = " + rate51200 + "  512000 = " + rate512000 + " 1024000 = " + rate1024000
            + " 5120000 = " + rate5120000 + "]";
}

}

I tried referring to this question which has similar requirements but couldn't quite get how to do it.

UPDATE 1:

The code I am using to deserialize is as follows, wherein I am passing the class as MeterRates.class:

public static <T> T unmarshalJSON(HttpEntity entity, Class<T> clazz) {
    InputStream is = null;

    try {
        return new Gson().fromJson(EntityUtils.toString(entity), clazz);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (null != is) {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Solution

  • You're trying to influence how Gson (a JSON mapper) deserializes an object by annotating its class with Jackson annotations (another, different JSON mapper).

    That can't possibly work. Gson doesn't care about Jackson annotations.

    If you want these annotations to be taken into account, use Jackson to deserialize your JSON. Here is a complete example serializing and deserializing an object using Jackson (I changed the type of the fields to Double, as that's what they should be):

        MeterRates rates = new MeterRates();
        rates.setRate1024(0.7654);
        rates.setRate51200(0.4567);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(rates);
        System.out.println("s = " + s);
        MeterRates m = objectMapper.readValue(s, MeterRates.class);
        System.out.println("m = " + m);