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

标签 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 的值坐标,它是一个 double 组。 感谢您的帮助。

最佳答案

coordinatesJSON 中的 JSONArray,因此您不能将其转换为 double!

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

关于android - org.json.JSONArray 无法转换为 double[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875524/

相关文章:

java - 我需要通过邮件从相机发送图像

java - 为什么此代码不能进行加法运算?

java - 使用 GSON 解析多层嵌套 json

java - 嵌套json用java转换非嵌套域

java - 如何使用 GSON 将 List 转换为 JSON 对象?

android - checkout 源代码后如何切换到特定版本的 Android?

android - 市场过滤器,如何排除布局-notlong

android - AdMob 中介中的待定事项

java - 使用 Gson 序列化具有非原始值的对象的 ArrayList?

javascript - 使用 jquery 将 json 字符串与下拉列表绑定(bind)