使用 json-lib 转换为 java 会抛出 ClassCastException

发布于 2024-11-06 20:07:26 字数 1673 浏览 0 评论 0原文

我正在使用 json-lib 将 json 对象转换为 java。 代码如下:

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;
    }
}

它抛出以下异常:

线程“main”中的异常 java.lang.ClassCastException:
net.sf.ezmorph.bean.MorphDynaBean 无法转换为 code.orgexample.json.B
在 code.orgexample.json.JsonConvertorDemo.main(JsonConvertorDemo.java:30)

有没有办法在 json-lib 中指定映射值的类类型?

非常感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

以歌曲疗慰 2024-11-13 20:07:26

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

又怨 2024-11-13 20:07:26

正如克罗所说:

这里说 fromObject 接受 JSON 格式的字符串、Maps、DynaBeans 和 JavaBeans

在我的代码中,ClassCastException 的原因是:

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

将代码更改为以下内容时,一切都按预期工作:

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

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);
    }
初相遇 2024-11-13 20:07:26

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);

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);
迷路的信 2024-11-13 20:07:26

为了尽快解决问题,请提交您的 JsonConverterDemo、A 和 B 类的所有代码。
特别是,缺少包声明、导入声明和行号阻碍了确定问题所在。

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.

别想她 2024-11-13 20:07:26

有没有办法在 json-lib 中指定地图值的类类型?

没有。反序列化为 List 时也是如此,即使您通过 toBean 调用告诉它您想要什么类型。

调用 toBean 后,集合中的值将为 DynaBeans。您必须迭代集合值并将它们转变为首选类型。变形可以手动完成,一次一个字段,也可以通过在 MorpherRegistry 中注册的 net.sf.ezmorph.Morpher 以更自动的方式完成。

警告:即使使用这种方法,在将值转变为目标类型的实例之前,您也必须小心如何引用该值。编译器(以及运行时)认为该值是参数化类型(如果使用泛型),因此它会很乐意尝试将其用作该类型。这当然会导致 ClassCastException (即使您的代码没有执行任何显式类型转换)。因此,在访问这些值时,只需声明并使用 Object 类型的引用即可获取它们。如果没有显式 Object 类型引用,请勿尝试以任何其他方式使用这些值。 (当你编写代码并看到错误时,你就会知道我在说什么。我现在太忙了,无法编写示例。)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文