Search code examples
androidjackson2gson

Json parsing using jackson or Gson


How to parse same json object keys to 2 different model classes using jackson or Gson?

This is input json

 {
      "last_sync_dt": "1486711867749",
      "meetings_info": [
        {
          "date": "2017-01-15",
          "meeting_id": "1",
          "subject": "Product Review with AUDI",
          "customer_id": "32",
          "customer_name": "David"
        }
      ]
    }  

These are model class

@JsonIgnoreProperties(ignoreUnknown = true)
Class MeetingInfo{
    @JsonProperty("date")
    private String date;
    @JsonProperty("meeting_id")
    private String meetingId;
    @JsonProperty("subject")
    private String subject;

    CustomerInfo customerinfo; 


//Other fields and getter setter


}

class CustomerInfo{
    @JsonProperty("customer_id")
    private String id;
   @JsonProperty("customer_name")
    private String name;

//Other fields and getter setter
}

Solution

  • please add object for global object

    Class ResultJson{
      String last_sync_dt;
      ArrayList<MeetingInfo> meetings_info;
    }
    

    and MeetingInfo will be

    public class MeetingInfo {
        private String date;
        private String meeting_id;
        private String subject;
        private CustomerInfo customerInfo;
    
        public void setDate(String date) {
            this.date = date;
        }
    
        public void setMeeting_id(String meeting_id) {
            this.meeting_id = meeting_id;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public void setCustomer(CustomerInfo customer) {
            customerInfo = customer;
        }
    }
    

    Customer info class

    public class CustomerInfo {
        private String customer_id;
        private String customer_name;
    
        public void setCustomerId(String customer_id) {
            this.customer_id = customer_id;
        }
    
        public void setCustomerName(String customer_name) {
            this.customer_name = customer_name;
        }
    }
    

    Meeting deserializer

    public class MeetingInfoAutho implements JsonDeserializer<MeetingInfo>{
    
        @Override
        public MeetingInfo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            JsonObject jObject = jsonElement.getAsJsonObject();
            MeetingInfo info = new MeetingInfo();
            CustomerInfo customer = new CustomerInfo();
            customer.setCustomerId(jObject.get("customer_id").getAsString());
            customer.setCustomerName(jObject.get("customer_name").getAsString());
            info.setDate(jObject.get("date").getAsString());
            info.setMeeting_id(jObject.get("meeting_id").getAsString());
            info.setSubject(jObject.get("subject").getAsString());
            info.setCustomer(customer);
            Log.e("info", jObject.toString());
            return info;
        }
    }
    

    and final call to json string to object

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(MeetingInfo.class, new MeetingInfoAutho());
    Gson gson = gsonBuilder.create();
    ResultJson resultObject = gson.fromJson(jsonStr, ResultJson.class);
    

    You should create MeetingInfoAutho which implements JsonDeserializer. Please find some examples about JsonDeserializer GSON for more info. This will give exact result.