无法将org.json.JSONObject强制转换为com.parse.ParseGeoPoint

时间:2015-03-16 04:42:44

标签: android json parse-platform

private class MyObject {
        private String itemName;
        private int itemPrice;
        private ParseGeoPoint location;
        private MyObject(JSONObject obj) {
            try {
                itemName = obj.getString("Item").toLowerCase().trim();
                itemPrice = obj.getInt("Price");
                location = (ParseGeoPoint) obj.get("Location");
            } catch (Exception e) {
                Utility.showMessage(e.getMessage(), "Oops!", SalesReportActivity.this);
            }
        }
        public String toString() {
            return "Name: " + itemName + "\n" +
                   "Price: " + itemPrice;
        }
    }

对于上面的代码,Android正在给我上面的例外。我也试过getJSONObject()但结果是一样的。我该怎么办?

1 个答案:

答案 0 :(得分:1)

obj.get("Location")返回JSONObject,因此首先从Location获取obj JSONObject以及latitudelongitude值:

            JSONObject jsonLocation = obj.getJSONObject("Location");
            double latitude=jsonLocation.getDouble("latitude");
            double longitude=jsonLocation.getDouble("longitude");   
            location=new ParseGeoPoint(latitude, longitude)