0

Below is my JSON data. I want to convert this to POJOs to store the Name,id,profession in a header table and the respective Jsonarray field in a child table.

JSON:
{
  "Name": "Bob",
  "id": 453345,
  "Profession": "Clerk",
  "Orders": [
    {
      "Item": "Milk",
      "Qty": 3
    },
    {
      "Item": "Bread",
      "Qty": 3
    }
  ]
}

Entity classes:

public class User {
        private String name;
        private Integer id;
        private String Profession;
        private JsonArray Orders;
        private UserCart userCart;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getProfession() {
            return Profession;
        }
        public void setProfession(String profession) {
            Profession = profession;
        }
        public JsonArray getOrders() {
            return Orders;
        }
        public void setOrders(JsonArray orders) {
            Orders = orders;
        }
        public UserCart getUserCart() {
            return userCart;
        }
        public void setUserCart(UserCart userCart) {
            this.userCart = userCart;
        }
    }

public class UserCart {
    private String item;
    private Integer qty;
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public Integer getQty() {
        return qty;
    }
    public void setQty(Integer qty) {
        this.qty = qty;
    }
}

But when I do below; I get error

Cannot deserialize instance of org.json.JSONArray out of START_ARRAY token

User user = new User();

JsonNode data = new ObjectMapper().readTree(jsonString);

user = headerMap.readValue(data.toString(), User.class);

How do I go about assigning the entire JSON to both the Java objects ?

2
  • 2
    You should have a class Order and the field private List<Order> orders. Btw, please skip to Java code conventions and have variable names in camelCase. Commented Jun 1, 2020 at 11:26
  • @Seelenvirtuose Thanks mate. This worked for me !!. I just cooked up that class manually to simulate my actual data so couldn't stick to conventions. But really great tip (y) Commented Jun 2, 2020 at 6:04

1 Answer 1

2

Use List<UserCart> for array data in json and use @JsonProperty for mapping different json node name to java object field. No need to use extra field (JsonArray Orders) anymore.

@JsonProperty("Orders")
private List<UserCart> userCart;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot. It worked. And the JsonProperty tip really helped to do the entity mapping with different field name. Cheers

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.