0

I have a string discounts with value:

"[{\"startInterval\":0,\"endInterval\":3,\"discount\":1000.0},
 {\"startInterval\":3,\"endInterval\":6,\"discount\":750.0},
 {\"startInterval\":6,\"endInterval\":9,\"discount\":500.0},
 {\"startInterval\":9,\"endInterval\":12,\"discount\":10.0}]"

My Discount class has model:

public class Discount  {

    private int startInterval;
    private int endInterval;
    private double discount;

    public Discount() {

    }

    public int getStartInterval() {
        return startInterval;
    }

    public void setStartInterval(int startInterval) {
        this.startInterval = startInterval;
    }

    public int getEndInterval() {
        return endInterval;
    }

    public void setEndInterval(int endInterval) {
        this.endInterval = endInterval;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

}

I want to serialize the string to List<Discount>. How do I do that? I have tried converting the string to JSONArray (gson and json), but I'm getting some errors.

Note that:

[{"startInterval":0,"endInterval":3,"discount":1000.0},
 {"startInterval":3,"endInterval":6,"discount":750.0},
 {"startInterval":6,"endInterval":9,"discount":500.0},
 {"startInterval":9,"endInterval":12,"discount":10.0}]

is stored in mysql db. I am fetching this into a string discounts, then trying to serialize it.

  • Serializing an object with GSON is pretty easy, have you tried [these methods](http://stackoverflow.com/questions/11038553/serialize-java-object-with-gson)? And [this](http://stackoverflow.com/questions/9186806/gson-turn-an-array-of-data-objects-into-json-android) one? – Oneiros Mar 29 '17 at 19:24
  • take a look at [jackson 2](http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/) JSON to Java Object... – PKey Mar 29 '17 at 19:27
  • @Oneiros These link serialize a single json element. What I need is serializing to List List rateList = mapper.readValue(disounts,TypeFactory.defaultInstance().constructCollectionType(List.class, Discount.class)); This is also not helping – Piyush Agarwal Mar 29 '17 at 19:32
  • @Plirkee Tried this List dicountList = mapper.readValue(disounts,TypeFactory.defaultInstance().cons‌​tructCollectionType(‌​List.class, Discount.class)); This calls the constructor of Discount class with paramters as disount string. The string contains the entire array. If it was single element, it could have been serialized – Piyush Agarwal Mar 29 '17 at 19:35
  • Look at the second link I provided, a list of objects is used. GSON automatically deals with Java collections. Am I missing something here? – Oneiros Mar 29 '17 at 19:36
  • @Oneiros Should work. I am getting an exception: IllegalStateException Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $ I'm doing Type type = new TypeToken>(){}.getType(); List inpList = new Gson().fromJson(discounts, type); where discounts has the value : "[{\"startInterval\":0,\"endInterval\":3,\"discount\":1000.0},{\"startInterval\":3,\"endInterval\":6,\"discount\":750.0},{\"startInterval\":6,\"endInterval\":9,\"discount\":500.0},{\"startInterval\":9,\"endInterval\":12,\"discount\":10.0}]" – Piyush Agarwal Mar 29 '17 at 19:45
  • @Oneiros Thanks it works now. My bad – Piyush Agarwal Mar 29 '17 at 19:52
  • You're welcome! I'll post the full answer – Oneiros Mar 29 '17 at 19:54

1 Answers1

1

With GSON:

String json = /* your json */
Type type = new TypeToken<List<Discount>>().getType();
List<Discount> discounts = new Gson().fromJson(json , type);
Oneiros
  • 4,328
  • 6
  • 40
  • 69