我现在正在 Eclipse 中使用 MongoDB 和 Java。我想通过 JSON 文档表示 Google map API 中的坐标。
我在 JSON 文档中有两个属性,纬度和经度,它们都是 double 的。但是当我这样做时:
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();
第二个,经度,给我这个错误:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
如何解决这个问题?
已解决:
感谢您尝试帮助我。 最后我解决了这个问题。问题在于导入,因为当我将 JSON 导入到 MongoDB 数据库时,该数据库会分配数据库所需的数据类型,因此某些纬度和经度数据的类型为 Int32,其他数据的类型为 Double。
问候。
最佳答案
Document.getDouble(String)
的文档说:
Throws: ClassCastException - if the value is not an double
该值显然是一个Integer
,因此请使用getInteger
。
关于java - JSON - Java Eclipse java.lang.ClassCastException : Integer cannot be cast to java. lang.Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50139359/