m pierre
m pierre

Reputation: 23

org.json.JSONArray cannot be cast to double[]

model:

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
                ]
            },

I am trying to recover the value coordinates of my json which is a double array. Thank you for your help.

Upvotes: 2

Views: 471

Answers (2)

mohammadReza Abiri
mohammadReza Abiri

Reputation: 1799

coordinates is a JSONArray in your JSON , so you cannot cast it to double!

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

Upvotes: 1

Vikas Rai
Vikas Rai

Reputation: 357

You're trying to cast jsonArray to Double. Use this geometry.setCoordinatesPoint(objectGeometry.getJSONArray("coordinates").getDouble(0),objectGeometry.getJSONArray("coordinates").getDouble(1));

Upvotes: 0

Related Questions