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.

8
  • Serializing an object with GSON is pretty easy, have you tried these methods? And this one? Commented Mar 29, 2017 at 19:24
  • take a look at jackson 2 JSON to Java Object... Commented Mar 29, 2017 at 19:27
  • @Oneiros These link serialize a single json element. What I need is serializing to List<Class> List<Discount> rateList = mapper.readValue(disounts,TypeFactory.defaultInstance().constructCollectionType(List.class, Discount.class)); This is also not helping Commented Mar 29, 2017 at 19:32
  • @Plirkee Tried this List<Discount> 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 Commented Mar 29, 2017 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? Commented Mar 29, 2017 at 19:36

1 Answer 1

1

With GSON:

String json = /* your json */
Type type = new TypeToken<List<Discount>>().getType();
List<Discount> discounts = new Gson().fromJson(json , type);
Sign up to request clarification or add additional context in comments.

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.