JSON-Java Eclipse Java.lang.ClassCastException:Integer不能转换为Java.lang.Double

JSON-Java Eclipse Java.lang.ClassCastException:Integer不能转换为Java.lang.Double,java,eclipse,mongodb,integer,double,Java,Eclipse,Mongodb,Integer,Double,我现在正在Eclipse中使用MongoDB和Java。我想用JSON文档在Google Maps的API中表示坐标 我在JSON文档中有两个属性,纬度和经度,它们都是双属性。但当我这么做的时候: List<Pair<Double, Double>> coordenadas = new ArrayList<Pair<Double, Double>>(); Pair<Double, Double> coordenadasAirpo

我现在正在Eclipse中使用MongoDB和Java。我想用JSON文档在Google Maps的API中表示坐标

我在JSON文档中有两个属性,纬度和经度,它们都是双属性。但当我这么做的时候:

List<Pair<Double, Double>> coordenadas = new ArrayList<Pair<Double, Double>>();
    Pair<Double, Double> coordenadasAirport;
    MongoCursor<Document> cursor = collection.find().iterator();
    try {
        while (cursor.hasNext()) {
            Double latitude = cursor.next().getDouble("latitude").doubleValue();
            Double longitude = cursor.next().getDouble("longitude").doubleValue();
如何解决这个问题

决议:

谢谢你试着帮助我。 最后我解决了这个问题。问题在于导入,因为当我将JSON导入MongoDB数据库时,该数据库会分配DB需要的数据类型,因此一些纬度和经度数据的类型为Int32,而其他数据的类型为Double

关于。

的文档说明:

抛出:ClassCastException-如果值不是双精度


该值显然是一个
整数
,因此请改用
getInteger

cursor.next().getDouble(“纬度”).doubleValue()返回双精度还是整数?我猜它返回double正如前面提到的,数据在存储器中显然是整数类型的。也就是说,它显然不应该用于位置数据,而且从MongoDB的角度来看,存储在单独的文档属性中也没有多大用处。如果希望数据库在任何时候对这些数据做任何有用的事情,那么最好将存储的文档更新到实际支持的任何一种存储表单。请参阅核心文档中的
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double