JSONObject from HashMap - Cast to String failed

依然范特西╮ 提交于 2019-12-25 09:48:48

问题


I need to create a JSONObject from a HashMap of a custom class's toString and a float value. I tried the code below hoping that it would just work:

public class MyClass {
    ...
    public String toString() {
        return "a nice string"
    }
}


HashMap<MyClass,Float> map = new HashMap<MyClass,Float>();
map.put(...);

JSONObject json = new JSONObject(map);

But I get:

java.lang.ClassCastException: MyClass cannot be cast to java.lang.String

What's the best way to create this JSONObject? Thanks.


回答1:


You need to change this:

HashMap<MyClass,Float> map = new HashMap<MyClass,Float>();

with

HashMap<String,Float> map = new HashMap<String,Float>();

as you said "HashMap of a custom class's toString and a float value"

You haven't mentioned how are you putting the values into the hashmap.

But if you using toString method of your custom class, then you should put it like :

MyClass m = new MyClass();
map.put(m.toString,123.45f);



回答2:


Seems like you're using the org.json library. If you take a look at the code of the JSONObject class, apparently they're not using generics.

public JSONObject(Map map) {
    this.map = new HashMap();
    if (map != null) {
        Iterator i = map.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry e = (Map.Entry)i.next();
            Object value = e.getValue();
            if (value != null) {
                this.map.put(e.getKey(), wrap(value));
            }
        }
    }
}

This map seems to handle entries with a String key and an Object value by the look of the keyPool map they use to manage unique String keys. In the comments, its also stated that:

This is used by JSONObject.put(string, object).

So it would be correct to assume the keys of the JSON objects are Strings.

Your MyClass type can't be upcasted to String directly (String is not a superclass of MyClass), that's why the constructor is actually complaining about the map, because it needs a map of the form HashMap<String,Object> (Note that there's no problem with Float and Object).

To fix the issue, you have to define a HashMap<String,Float> where you should store a String representation of your MyClass object either by using toString.

If you can't use a String you can consider using an intermediate structure that maps a code represented with a String to a certain MyClass object, so you can retain your MyClass class.




回答3:


Both Gamb's and Abu's answers are correct and helped me to get to my final result.

I solved my problem like this:

HashMap<MyClass,Float> obj = functionThatReturnsThisStructure();

JSONObject jsonObj = new JSONObject();

for (Entry<MyClass,Float> entry: obj.entrySet()) {
    jsonObj.put(entry.getKey().toString(), entry.getValue());
}


来源:https://stackoverflow.com/questions/14963770/jsonobject-from-hashmap-cast-to-string-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!