org.json.JSONArray无法转换为double []

时间:2019-11-15 10:54:58

标签: android gson

型号:

public class Geometry {

    public String type;
    public List<Position> coordinatesRoute;
    public double[] coordinatesPoint;



method for recover jsonObjet
 JSONObject objectResponse = new JSONObject(res);
                                JSONArray JarrayFeatures  = objectResponse.getJSONArray("features");


                                for (int i = 0; i < JarrayFeatures.length(); i++) {
                                    JSONObject jsonObjFeatures = JarrayFeatures.getJSONObject(i);
                                    Log.e("jsonObjFeatures:", " " + jsonObjFeatures);
                                    //geoFeatures.setType((String) jsonObjFeatures.get("type"));
                                    features.setType((String) jsonObjFeatures.get("type"));
                                    JSONObject objectGeometry = jsonObjFeatures.getJSONObject("geometry");

                                    geometry.setType((String) objectGeometry.get("type"));

                                    Log.e("objectGeometry:", "" + objectGeometry);

                                //problems  
                       geometry.setCoordinatesPoint((double[]) objectGeometry.get("coordinates"));

JSON:

"features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    2.1163,
                    48.8288
                ]
            },

我试图恢复我的json的值坐标,它是一个双精度数组。 谢谢您的帮助。

2 个答案:

答案 0 :(得分:1)

coordinates是您的JSONArray中的JSON,因此无法将其强制转换为double

 geometry.setCoordinatesPoint(objectGeometry.getJSONArray("coordinates").getDouble(0),objectGeometry.getJSONArray("coordinates").getDouble(1));

答案 1 :(得分:0)

您正在尝试将jsonArray转换为Double。使用此geometry.setCoordinatesPoint(objectGeometry.getJSONArray("coordinates").getDouble(0),objectGeometry.getJSONArray("coordinates").getDouble(1));