使用fasterxml从JSON创建Java对象时发生异常

使用fasterxml从JSON创建Java对象时发生异常,java,json,jackson,fasterxml,Java,Json,Jackson,Fasterxml,我正在尝试使用fasterxml和下面的JSON构建一个Java对象 JSON : {"name":"Location Test", "location":{ "coordinates":[10.1234657,10.123467] }, ... } 我得到一个例外: play.api.Application$$anon$1: Execution exception[[Run

我正在尝试使用fasterxml和下面的JSON构建一个Java对象

    JSON : {"name":"Location Test",
            "location":{
                "coordinates":[10.1234657,10.123467]
            },
            ...
    }
我得到一个例外:

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"])]]
地点类别:

public class Place{
   private String name;
   private Location location;
   ...
   getters and setters
}
地点类别:

public class Location{
   private double[] coordinates;

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

有人能告诉我是什么导致了这个问题吗?

您需要从location对象中删除构造函数。我已经根据您提供的信息创建了示例程序,并成功运行

位置类别:

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;
 }
} 
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());
}
}
地点类别:

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;
 }
} 
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());
}
}
应用程序类: 公共类应用程序 { 公共静态void main(字符串[]args)引发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-包含JSON

{
   "name":"Location Test",
        "location":{
            "coordinates":[10.1234657,10.123467]
   }
}
您需要在maven项目中包含以下依赖项:

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

com.fasterxml.jackson.core
杰克逊数据绑定
2.6.1

我已经为places类中的Location编写了setter,以接收双数组而不是Location对象。对其进行了更改。现在工作正常。无需编辑Location的构造函数。谢谢先生。