【问题标题】:Spring for android: How to parse Json objects with different parametersSpring for android:如何解析具有不同参数的 Json 对象
【发布时间】:2013-08-19 09:48:51
【问题描述】:

我得到以下 Json:

"geometries": [
                {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                4.8979805,
                                52.3798389
                            ],
                            [
                                4.8982922,
                                52.3801447
                            ],
                            [
                                4.9027811,
                                52.378504
                            ]
                        ]
                    ]
                },
                {
                    "type": "Point",
                    "coordinates": [
                        4.7622823,
                        52.3095072
                    ]
                },
                {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [
                                4.4665891,
                                51.9253793
                            ],
                            [
                                4.4700603,
                                51.926059
                            ],
                            [
                                4.4707517,
                                51.9247593
                            ],
                            [
                                4.4706054,
                                51.9247303
                            ]
                        ]
                    ]
                }
            ]
        }

可以有PolygonsPoints。我正在使用spring for android来解析Json。 但是因为 geometries 有不同的 coordinates 类型(双精度数组的数组或只是双精度数组),我不知道该怎么做。

Json 如果来自外部来源,所以不能做太多。

谁能帮帮我?

提前致谢

【问题讨论】:

    标签: android json spring


    【解决方案1】:

    您可以将坐标定义为; List<Object> coordinates; 在使用坐标之前(即在 getter 方法中),您可以检查您是否对其进行了后期处理,并且在后期处理中您可以进一步进行。

    我想这会对你有所帮助

    class MyClass {
        Geometry geometries[];
    
        Geometry[] getGeometries() {
            return geometries;
        }
    
        void setGeometries(Geometry[] geometries) {
            this.geometries = geometries;
        }
    }
    class Geometry {
        String type;
        List<Object> coordinates;
    
        String getType() {
            return type;
        }
    
        void setType(String type) {
            this.type = type;
        }
    
        List<Object> getCoordinates() {
            return coordinates; // you can simply return something more advanced such like List<CoordinateInterface> 
        }
    
        void setCoordinates(List<Object> coordinates) {
            this.coordinates = coordinates;
        }
    }
    

    【讨论】:

    • 考虑使用更好的用户名 :) 人们不喜欢在这里回答游牧民族 :)
    • 感谢您的提示,这是我在这里问的第二个问题,我通常从这里回答的其他问题中得到答案,所以我真的没有考虑过更改我的用户名。
    【解决方案2】:

    您可以检查与键coordinates关联的值的类型

    并且根据值的类型,你可以决定行为。

    您可以使用instanceof检查类型

    所以在你的情况下,你可以像这样检查->

    if(object instanceof List) {
     //Array of Array of Array of Double
    } else if(object instance of Map) {
     //Array of Double   
    }
    

    您可以使用此代码 -

    Map<String, Object> jsonMap = gson.formJson(jsonStr, Map.class);
    List<Map<String, Object>> list = jsonMap.get("geometries");
    foreach(Map<String, Object> map : list) {
     List<Object> objList = map.get("coordinates");
     foreach(Object obj : objList) {
      if(obj instanceof List) {
       //obj is Array of Double
      } else if(obj instace of Double){
       //obj is Double/Actual value
      }
     }
    }
    

    【讨论】:

    • 坐标键是一个字符串值。我知道如何使用 instanceof,问题是 Spring 检查变量名,然后将值放入正确的值。我不知道如何使用 2 种具有相同名称的不同类型来修复它。所以我需要知道如何告诉 Spring 将正确的类型连接到坐标变量
    • 使用 Spring for Android 解析 JSON 是什么意思?你是在用 Jackson 来解析 JSON 吗?
    • springsource.org/spring-android 它是一个在 Android 上解析 Json 的库。我使用 Gson 来解析 Json,Spring 是...
    • 解决问题了吗?
    • 我使用了其他解决方案,使用 spring for android 更容易实现。还是谢谢!
    猜你喜欢
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2017-03-15
    • 2016-02-17
    相关资源
    最近更新 更多