1

How can I convert a Json list in to a List of objects with javax.json? This is the Json

{"chartId":O1234567,"products":[{"productId":1,"productInfo":"wine"},{"productId":2,"productInfo":"cookies"},{"productId":3,"productInfo":"donut"}]}

And this are my classes.

public class Chart{
        private int chartId;
        private List<Product> products;

        //getters and setter
        }

  public class Product
        {
        private int productId;
        private String productInfo;
    }


  public class ConvertJson{
         public Chart convertJsonToChart(String JsonToConvert){

            InputStream fis=new FileInputStream(jsonToConvert);        
            JsonReader jsonReader = Json.createReader(fis);       
            JsonObject jsonObject=jsonReader.readObject();       
            jsonReader.close();
            fis.close();

            Chart chart=new chart();
            chart.setChartId(jsonObject.getInt("productId"));
           //Here I should convert the productc json list to an java array list


           }
   }

Like u see I do know how to convert the chart json to chart java object, but how to I do that if I want to convert a json list? Thank you

Claudiu Matei
  • 4,091
  • 3
  • 19
  • 33

1 Answers1

0

As Christoph said, you can use Jackson. Here is a related topic that shows you how to use the library. First create an object mapper and then read the value from json passed as a parameter in the method readValue() of the mapper.

How to use Jackson to deserialise an array of objects

You can read more examples about Jakson here: http://www.tutorialspoint.com/jackson/

Or you can do deeper research of this library.

Community
  • 1
  • 1
user5455130
  • 78
  • 1
  • 8