0

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);
        }
    }
}
3
  • Post your code. And respect the Java naming conventions: variables start with a lowercase letter. Commented Aug 22, 2015 at 12:06
  • Also, why do you use String, since the values are numbers? Why not use Double instead? Commented Aug 22, 2015 at 12:13
  • @JBNizet: Sorry for the variable name goof-up, changed that and added code I am using to deserialize. Commented Aug 22, 2015 at 12:20

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I am using Gson to deserialize the complete JSON structure, is it possible to tweak the solution to work with Gson instead?
I'm not used to Gson, but it has a good documentation. Read it. A 2 minutes glance at the TOC of the user guide was enough to find this: sites.google.com/site/gson/…
I removed jackson annotations with GSON annotations (@SerializedName) and it works now. Thanks!
-1

You can make use of Jackson JSON API.

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.