0

I am trying to build a Java object using fasterxml with the below JSON

    JSON : {"name":"Location Test",
            "location":{
                "coordinates":[10.1234657,10.123467]
            },
            ...
    }

I am getting this exception :

play.api.Application$$anon$1: Execution exception[[RuntimeException: com.fasterxml.jackson.databind.JsonMappingException:
Can not deserialize instance of double[] out of START_OBJECT token
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.mypackages.models.Place["location"])]]

Place Class :

public class Place{
   private String name;
   private Location location;
   ...
   getters and setters
}

Location Class :

public class Location{
   private double[] coordinates;

   public Location(double[] coordinates) {
       this.coordinates = coordinates;
   }
   ...
   //getter and setter for coordinate field
} 

Can someone tell me what is causing the issue?

Shabin Muhammed
  • 1,092
  • 2
  • 18
  • 29

1 Answers1

2

You need to remove the constructor from the location object. I have created the sample program as per the information given by you and it ran successfully.

Location Class:

public class Location{
private double[] coordinates;

/**
 * @return the coordinates
 */
public double[] getCoordinates() {
    return coordinates;
}

/**
 * @param coordinates the coordinates to set
 */
public void setCoordinates(double[] coordinates) {
    this.coordinates = coordinates;
 }
} 

Place Class:

public class Place{
private String name;
private Location location;
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
 * @return the location
 */
public Location getLocation() {
    return location;
}
/**
 * @param location the location to set
 */
public void setLocation(Location location) {
    this.location = location;
}


@Override
public String toString() {
    return "Place: " + name + " Location: " + Arrays.toString(location.getCoordinates());
}
}

APP Class: public class App { public static void main(String[] args) throws IOException {

      //read json file data to String
      byte[] jsonData = Files.readAllBytes(Paths.get("places.txt"));

      //create ObjectMapper instance
      ObjectMapper objectMapper = new ObjectMapper();

      Place place = objectMapper.readValue(jsonData, Place.class);

      System.out.println("Place Object\n"+ place);
    }
}

Places.txt - containing JSON

{
   "name":"Location Test",
        "location":{
            "coordinates":[10.1234657,10.123467]
   }
}

You need to include following dependencies in maven project:

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.6.1</version>
</dependency>
Amit Tamrakar
  • 512
  • 3
  • 13
  • I had written the setter for Location in places class to receive a double array instead of a Location object. Changed it. Now working fine. Didn't have to edit the Constructor of Location. Thank you sir. – Shabin Muhammed Aug 16 '15 at 07:09