'I have the following json I want fetch the record one by one record using JsonObject after parse

I have the the following JSON and the following code I am getting object the object but how to fetch one by one and how to set in POJO class?it is basically google direction map api response i want to store only legs and waypoints

{
"legs":[
{"end_address":"Uttam Nagar, Delhi, 110059, India",
      "end_location":{
         "lat":28.6195607,
         "lng":77.0550097
      },
      "start_address":"Delhi, India",
      "start_location":{
         "lat":28.7040873,
         "lng":77.1024072
      }
}

]}


@PostMapping("/create")
    public ResponseEntity<ResponseWrapper> savedRoutedata(@RequestBody MapRequest data) throws JsonProcessingException {
        
         ObjectMapper mapper=new ObjectMapper();
         String jsonContent = mapper.writeValueAsString(data);
 JsonObject o=new JsonParser().parse(jsonContent).getAsJsonObject();     
         
         System.out.println("My Legs:-"+o.get("legs"));
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MapRequest {
    
    
    @JsonProperty("legs")
    private List<Legs> legs;
        
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonProperty("overview_path")
    private List<WaypointPath>wayPoint;

}



Solution 1:[1]

In case your POJOs are correct, your MapRequest should already have everything set and you can use them as any Java object, without the need to convert them however.

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;



import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Controller
public class MyTestController {

  @GetMapping("/create")
  public String savedRoutedata(@RequestBody MapRequest data) {

      for(Legs leg : data.getLegs()) {
        System.out.println(leg);
      }
    return null;
  }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
class MapRequest {

  @JsonProperty("legs")
  private List<Legs> legs;


}

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
class Legs {
  @JsonProperty("end_address")
  private String endAddress;
  @JsonProperty("start_address")
  private String startAddress;
  @JsonProperty("end_location")
  private Location endLocation;
  @JsonProperty("start_location")
  private Location startLocation;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
class Location {
  @JsonProperty("lat")
  private Double lat;
  @JsonProperty("lng")
  private Double lng;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 andra.ioana