开发者

Using json-lib to transform to java throws ClassCastException

I am using json-lib to transform json object to java. The code is as below:

public class JsonConvertorDemo {

    public static void main(String[] args) {
        B b1 = new B("b1");
        Map<String, B> bMap = new HashMap<String, B>();
        bMap.put("key1", b1);
        A a1 = new A(bMap);

        JSONObject jsonObject = JSONObject.fromObject(a1);
        String json = jsonObject.toString();
        jsonObject = JSONObject.fromObject(json);

        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("bMap", Map.class);
        a1 = (A) JSONObject.toBean(jsonObject, A.class, classMap);

        bMap = a1.getbMap();
        System.out.println(bMap.get("key1").getB1());
    }
}

public class A {
    private Map<String, B> bMap = new HashMap<String, B>();
    public A() {}
    public A(Map<String, B> bMap) {
        this.bMap = bMap;
    }
    public Map<String, B> getbMap() {
        return bMap;
    }
    public void setbMap(Map<String, B> bMap) {
        this.bMap = bMap;
    }
}

public class B {开发者_运维技巧
    private String b1;
    public B() {}
    public B(String b1) {
        this.b1 = b1;
    }
    public String getB1() {
        return b1;
    }
    public void setB1(String b1) {
        this.b1 = b1;
    }
}

It throws the following exception:

Exception in thread "main" java.lang.ClassCastException:

net.sf.ezmorph.bean.MorphDynaBean cannot be cast to code.orgexample.json.B

at code.orgexample.json.JsonConvertorDemo.main(JsonConvertorDemo.java:30)

Is there a way to specify class type of a map's value in json-lib?

Many thanks for any help.


It's says here that fromObject accepts JSON formatted strings, Maps, DynaBeans and JavaBeans


As chro said:

It's says here that fromObject accepts JSON formatted strings, Maps, DynaBeans and JavaBeans

In my code, the ClassCastException was caused by:

for (TheClass childNode : command.getChildren()) {

When changing the code to the following, everything worked as expected:

for (Object childNode : command.getChildren()) {
        JSONObject fromObject = JSONObject.fromObject(childNode);
        TheClass childCommand = (TheClass) JSONObject.toBean(fromObject,
            TheClass.class);
    }


http://hw1287789687.iteye.com/admin/blogs/1993048

JsonConfig jsonConfig = new JsonConfig();

    jsonConfig.setRootClass(Class2.class);
    Map<String, Class> classMap = new HashMap<String, Class>();
    classMap.put("students", Student.class); // 指定JsonRpcRequest的request字段的内部类型
    jsonConfig.setClassMap(classMap);


To solve the problem sooner, please submit all your code of classes JsonConverterDemo, A and B. Especially, the lack of the package declaration, the import statements and line numbers obstruct to determine what is the problem.


Is there a way to specify class type of a map's value in json-lib?

Nope. Same when deserializing to a List<CustomType>, even if you told it what type you want with the toBean call.

After the call to toBean, the values in the collection will be DynaBeans. You have to iterate through the collection values and morph them into the preferred types. The morphing can be done manually, a field at a time, or in a more automatic fashion with a net.sf.ezmorph.Morpher registered in the MorpherRegistry.

WARNING: Even with this approach, you have to be careful about how you reference the value before you morph it to an instance of the target type. The compiler (and thus the runtime) thinks the value is of the parameterized type (if using generics), and so it will gladly try to use it as that type. This of course causes a ClassCastException (even if your code doesn't do any explicit type casting). So, when accessing the values, just get to them by declaring a reference of type Object and using it. Don't try to use the values in any other way without the explicit Object type reference. (You'll know what I'm talking about when you write the code and see the errors. I'm too busy to code an example, right now.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜