使用 json-lib 转换为 java 会抛出 ClassCastException
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它说这里 fromObject 接受 JSON 格式的字符串、Maps、DynaBeans 和 JavaBeans
It's says here that fromObject accepts JSON formatted strings, Maps, DynaBeans and JavaBeans
正如克罗所说:
在我的代码中,ClassCastException 的原因是:
将代码更改为以下内容时,一切都按预期工作:
As chro said:
In my code, the ClassCastException was caused by:
When changing the code to the following, everything worked as expected:
http://hw1287789687.iteye.com/admin/blogs/1993048
http://hw1287789687.iteye.com/admin/blogs/1993048
为了尽快解决问题,请提交您的 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.
没有。反序列化为
List
时也是如此,即使您通过toBean
调用告诉它您想要什么类型。调用
toBean
后,集合中的值将为DynaBeans
。您必须迭代集合值并将它们转变为首选类型。变形可以手动完成,一次一个字段,也可以通过在MorpherRegistry
中注册的net.sf.ezmorph.Morpher
以更自动的方式完成。警告:即使使用这种方法,在将值转变为目标类型的实例之前,您也必须小心如何引用该值。编译器(以及运行时)认为该值是参数化类型(如果使用泛型),因此它会很乐意尝试将其用作该类型。这当然会导致 ClassCastException (即使您的代码没有执行任何显式类型转换)。因此,在访问这些值时,只需声明并使用
Object
类型的引用即可获取它们。如果没有显式Object
类型引用,请勿尝试以任何其他方式使用这些值。 (当你编写代码并看到错误时,你就会知道我在说什么。我现在太忙了,无法编写示例。)Nope. Same when deserializing to a
List<CustomType>
, even if you told it what type you want with thetoBean
call.After the call to
toBean
, the values in the collection will beDynaBeans
. 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 anet.sf.ezmorph.Morpher
registered in theMorpherRegistry
.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 explicitObject
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.)