0

What is the code for take key-values from given json, We can use Hash Map? how to write the code?Need to change pojo class?I need to take 'startTime' and 'endTime' from JSON

My input Json 
    {
    "active" : 0,
    "id" : 3,
    "doctorId" :8,
    "hospitalId" : 55,
    "timeSlot" : [
         { "startTime": "10",
           "endTime": "12" 
         },
         { "startTime": "3",
           "endTime": "5" 
         }
        ]
    }

My code

List slot = customDuty.getTimeSlot();
                    int count = 0;      
                    while (slot.size() > count) {
                        logger.info("checking"+slot.get(count));
                       count++;

                    }

PojoClass

private List timeSlot ;

    public List getTimeSlot() {
        return timeSlot;
    }
    public void setTimeSlot(List timeSlot) {
        this.timeSlot = timeSlot;
    }

output

checking{startTime=10, endTime=12}
checking{startTime=3, endTime=5}
2
  • Output of what code? What is the type if TimeSlot? Commented Dec 20, 2019 at 10:03
  • Basics of this would be : deserialise it, if you do it correctly you can easily access TimeSlot Commented Dec 20, 2019 at 10:07

2 Answers 2

1

If you are working with SpringBoot java you need to add Jackson dependencies in your POM

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>

And then you need map the json file with your POJO

public class ObjectMapperDemo {
 public Hospital readJsonWithObjectMapper() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper(); 
    Hospital hos = objectMapper.readValue(new File("hospital.json"), Hospital.class);
    return hospital;
} 
}

The hospital entity class is define as follows:

import java.util.List;

public class Hospital {
private boolean active;
private int id;
private int doctorId;
private int hospitalId;
private List<TimeSlot> timeSlot;`

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getDoctorId() {
    return doctorId;
}

public void setDoctorId(int doctorId) {
    this.doctorId = doctorId;
}

public int getHospitalId() {
    return hospitalId;
}

public void setHospitalId(int hospitalId) {
    this.hospitalId = hospitalId;
}

public List<TimeSlot> getTimeSlot() {
    return timeSlot;
}

public void setTimeSlot(List<TimeSlot> timeSlot) {
    this.timeSlot = timeSlot;
}

@Override
public String toString() {
    return "Hospital{" +
            "active=" + active +
            ", id=" + id +
            ", doctorId=" + doctorId +
            ", hospitalId=" + hospitalId +
            ", timeSlot=" + timeSlot +
            '}';
}

}

Sign up to request clarification or add additional context in comments.

Comments

0

The most java style would be to create a TimeSlot pojo

class TimeSlot {

  private int startTime;
  private int endTime;

  // getters and setters
}

And then have in your main entity

class MainEntity {

  private List<TimeSlot> timeSlot;

  // getters and setters
}

And then you let spring (jersey) parse it for you.

Usage:

mainEntity.getTimeSlots().foreach(timeSlot -> logger.log(
   "checking\{startTime={}, endTime={}\}",
   timeSlot.getStartTime(),
   timeSlot.getEndTime(),
))

But indeed you could take the approach of having a HashMap<String, Integer> as the type of timeSlot, and access trough its keys.

class MainEntity {

  private List<HashMap<String, Integer>> timeSlot;

  // getters and setters
}
mainEntity.getTimeSlots().foreach(timeSlot -> logger.log(
   "checking\{startTime={}, endTime={}\}",
   timeSlot.get("startTime"),
   timeSlot.getEndTime("endTime"),
))

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.