【问题标题】:GsonRequest has problems to parse a MapGsonRequest 在解析 Map 时遇到问题
【发布时间】:2014-09-01 01:33:50
【问题描述】:

我使用 GsonRequest 来序列化我的数据。但是我有这种情况:

这是 JSON 格式的数据:

[...] 
"u": {
      "53bde5b5e4fc4978c0000015": {
          "la": 40.772673,
          "lo": 9.6657388
     }, ...

我使用以下方法序列化数据:

public class ClusterResult extends SingleElement{
    public Integer count;
    public ClusterData[] c;
    public HashMap<String, ClusterUnit> u;
}

(跟Map一样)

知道如何改变它吗?我使用 GsonRequest 类来序列化数据(https://gist.github.com/ficusk/5474673)。

谢谢:)

【问题讨论】:

    标签: android json gson android-volley


    【解决方案1】:

    您为什么不尝试将ClusterUnit 包装在一个类中并使用自定义反序列化器?也许我不明白你的问题是什么,如果是这样,请告诉我。我就是这样做的:

    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParseException;
    import java.lang.reflect.Type;
    import java.util.Map;
    
    /**
     *
     * @author astinx
     */
    public class Test {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Test().run();
        }
    
        public class ClusterUnitWrapped {
            String id; //53bde5b5e4fc4978c0000015
            Double la;
            Double lo;
    
            private ClusterUnitWrapped(String id, Double la, Double lo) {
                this.id = id; this.la = la; this.lo = lo;
            }
    
            @Override
            public String toString() {
                return "ClusterUnitWrapped{" + "id=" + id + ", la=" + la + ", lo=" + lo + '}';
            }
    
        }
    
        public class ClusterUnitRequest {
            //...
            ClusterUnitWrapped u;
    
            //...
            @Override
            public String toString() {
                return "ClusterUnitRequest{" + "u=" + u + '}';
            }
        }
    
        private class ClusterUnitDeserializer implements JsonDeserializer<ClusterUnitWrapped> {
            public ClusterUnitWrapped deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
                for (Map.Entry<String, JsonElement> map : json.getAsJsonObject().entrySet()) {
                    String id = map.getKey().toString(); //Obviously is going to iterate only once.
                    Double la = map.getValue().getAsJsonObject().get("la").getAsDouble();
                    Double lo = map.getValue().getAsJsonObject().get("lo").getAsDouble();
                    return new ClusterUnitWrapped(id, la, lo);
                }
                return null;
            }
        }
    
        private void run() {
            String json = "{\"u\":{\"53bde5b5e4fc4978c0000015\": { \"la\": 40.772673, \"lo\":9.6657388}}}";
            Gson gson = new GsonBuilder()
                    .registerTypeAdapter(ClusterUnitWrapped.class, new ClusterUnitDeserializer())
                    .create();
            ClusterUnitRequest u = gson.fromJson(json, ClusterUnitRequest.class);
            System.out.println(u.toString());
        }
    
    }
    

    【讨论】:

    • 这不是我使用的代码的实际版本,但它真的很鼓舞人心,谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2011-03-16
    相关资源
    最近更新 更多