【问题标题】:Exception while creating Java object from JSON using fasterxml使用 fastxml 从 JSON 创建 Java 对象时出现异常
【发布时间】:2015-11-09 00:22:52
【问题描述】:

我正在尝试使用 fastxml 和下面的 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
} 

谁能告诉我是什么导致了这个问题?

【问题讨论】:

    标签: java json jackson fasterxml


    【解决方案1】:

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

    位置类:

    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());
    }
    }
    

    APP类: 公开课App { public static void main(String[] 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>
    

    【讨论】:

    • 我在 places 类中编写了 Location 的 setter 来接收双精度数组而不是 Location 对象。改变了它。现在工作正常。不必编辑位置的构造函数。谢谢先生。
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 2015-03-20
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    相关资源
    最近更新 更多