TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
7 W4 a* W9 _9 V/ n) Z8 R) y但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。
1 h8 _" S! s5 f解决方法:- public class Student implements java.io.Serializable{. J C" _% ]9 _& y
- 0 V% Q# R& D6 k5 \) j3 ~& p1 n7 h7 A
- /** 8 v0 c' Y0 ]& v4 G8 E! I
- * @Fields serialVersionUID : TODO
. H* p3 \1 `# Z3 B" i4 A - */
3 K4 C5 J: `5 P: E - private static final long serialVersionUID = -1943961352036134112L;# S) K0 I; K5 A W
- private String sname;
; d. u' |# k$ ^6 h X - private Integer age;
, I9 \$ [: |9 a! |9 d - public String getSname() {
0 L9 j3 c* }- |7 p" R# Q/ R - return sname;' a ^8 h' r1 _ N) ^5 Q3 @
- }$ ?* W3 ]# t# @
- public void setSname(String sname) {
2 Y* s" _( y Q) j4 W8 c |5 G- P - this.sname = sname;5 w4 k3 D8 l7 Z
- }1 A/ B) w: i6 I1 m* c8 i; N& c# O
- public Integer getAge() {
[0 v S! A8 ~( O Q - return age;; M8 r( V4 f8 u! v! A4 o
- }
) ?' A8 N0 l# \- J. D1 T. l - public void setAge(Integer age) { G( O7 t. S3 r
- this.age = age;7 w- r" o6 ?, }% W. V! U
- }
- Q6 p+ Z, C* i -
* L4 r t! M+ E0 M+ q, J - }
0 _& [1 ^9 I( K1 u4 U
复制代码 由字符串转java bean时可以使用以下方法:6 K' X0 R5 U _4 q7 P
4 @% N9 S E5 z' e' t2 o n0 n- JSONObject jobj = JSONObject.fromObject(str);
! _% g$ U& b" k4 }0 t% @) n' y - Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码
. x' T4 l: y" N0 q: c, m是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……
# @9 U j# Y# `6 _9 I 在JSONObject.toBean的时候如果转换的类中有集合,可以先定义Map<String, Class> classMap = new HashMap<String, Class>();在classMap中put你要转换的类中的集合名,像:classMap.put("teachers", Teacher.class);然后在toBean()的时候把参数加上, 像:Student student=(Student) JSONObject.toBean(str, Student.class, classMap);1 i5 B% H! z, @2 g L! i
9 ]1 D( b9 Z. L2 e; R( w p
比如我定义了如下类型:% C4 u3 @( ^# }6 |0 o
- import java.util.List;8 Q- _8 H. ?. T: L# a! N4 P
- /**% L' @ ^9 H/ M+ {5 N7 W
- * ! y( ?: X# o3 q* R0 p h* Y4 S5 u
- * <P>Tiltle: AnalysisModel.java </P>
4 n- l1 V3 `+ N4 Y k$ i. H - * <P>Description:模型类 </P>/ ]/ [) t3 X' X# V
- * @author biyutong# r/ a2 f& l0 K% B9 N9 p
- * @date Jun 11, 2012 9:37:54 AM
, Q2 |$ C" t* O - * @version:V1.0
% _8 L: o; P2 y' _2 x3 } - */
9 Y2 Q" K( p1 f# M9 B i; h - public class AnalysisModel implements java.io.Serializable{4 x9 R# s2 ` I0 v
- 0 o7 b8 h0 H9 `
- /**
" w9 k- p7 L7 y5 r - * 固定条件+ b8 y Z9 n; i$ V$ f
- */
$ C! R* p9 W; N. | - private List<Condition> regularCondtionList;//: A) G" f) _6 q% H
- /**
$ L) R A- p% v& i" P$ h' ` - * 动态条件
% R' ~% q3 }: M) T7 j( m - */
2 l+ G+ ^) n- M" _ - private List<ConditionField> dynamicConFieldList;3 Y. }6 G: T% ~- D+ {& I: O0 w
- /**- B+ @4 @/ [0 g7 Z! a2 C
- * 结果0 @8 c' U6 U) r i: Z) _0 g7 I
- */
" W0 w% }/ i+ ^/ a2 P - private List<ResultField> resultField;
/ i' M) C4 c% k7 R( r4 S2 X/ e - /**! S, P2 I6 B' C
- * 获取前多少条数据
, l) g) y: @' Y( M7 q6 L - */
9 G, P" X4 C! }4 V! ^% m - private Integer topnum;
" s( |: b9 w- v; ]; R4 ` - /**
7 z. s7 l2 X f9 t - * 顺序还是随机
1 d. ] k! Q* f( r! b4 Y$ f - */3 C" Z3 V/ P9 _0 h& ]
- private String sortFlag;4 Y+ b, \( @) O2 q5 n1 [
- /**8 D/ ?6 [$ V: K; S" K
- * 备注
) `6 Y' T" r& F2 ~; m - */5 c/ s9 U$ K; E
- private String remark;% H6 \% ]) ^. s$ G; X) i
- /**& G0 |6 q, u1 }/ u% g# U
- * 模型名称
7 f+ p: S+ R( \ s' l7 D0 ~ - */
- V' ]7 F1 Z* J - private String modelName;4 F( Q! o4 c( P1 e0 s) K
- /**
9 l! k/ s4 J" J) V* Z - * 对应的xml文件名称
# o8 ~$ i8 U Z' N" R8 C' F - */
& L: }* g2 _: X" J d: P4 X% U - private String fileName;
% a% ?4 w* t% u6 t+ ` - public List<Condition> getRegularCondtionList() {
' @- J3 ^& O+ A3 O8 t - return regularCondtionList;
- h, o) C/ _) F& P4 y. U9 S8 j - }
2 u( E, g1 J' C n2 X - public void setRegularCondtionList(List<Condition> regularCondtionList) {7 E) |$ y% `1 Y) e9 H" M& h9 ]& o. h$ a
- this.regularCondtionList = regularCondtionList;
3 l$ F) j0 e, u - }* x+ E- W% k% M
- public List<ConditionField> getDynamicConFieldList() {
* B t; V) A8 p& q- D* p - return dynamicConFieldList;- ~3 [, T/ `1 O: S( y3 U: I1 T
- }
6 d$ d6 y6 q$ U) @ - public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {& Z" U. A/ f) C
- this.dynamicConFieldList = dynamicConFieldList;
: \* o7 t2 E0 k( n5 h1 n0 _ - }
# L2 f( |; W+ Q; E - public Integer getTopnum() {
9 P% ~+ \3 j8 N* b: a8 O2 } - return topnum;) ?0 Q' f z: N, t0 Z% i6 {
- }9 c# Y6 o P; ]: q( U
- public void setTopnum(Integer topnum) {
/ V8 o1 a, k- B/ _+ R5 U - this.topnum = topnum;
t5 h+ |9 O3 M' |$ ` - }5 K8 i) F6 O1 Z2 {4 L: e
- public String getSortFlag() {
* M& P( G8 l* C" u* Q - return sortFlag;% m3 b! `% x6 b- n* ?; \6 Q
- }8 o3 b& x$ k# F5 J9 B
- public void setSortFlag(String sortFlag) {# h5 D/ r9 S9 O5 |+ u
- this.sortFlag = sortFlag;
/ V0 B6 C1 W4 q2 G! @, R - }
; X; y; I3 r% t" I% j - public String getRemark() {8 J& u7 x7 r, b6 }; @+ k
- return remark;2 z8 `1 Y0 S2 i3 C6 v" X
- }
2 \ \- b4 p3 r0 ?1 X7 z ? - public void setRemark(String remark) {" I0 C* S$ `5 q: f# q
- this.remark = remark;5 C$ E' ~9 x# L: [: C
- }2 N, G8 H" E3 O
& \+ @# h. `& \+ l1 Q5 z- ) Z( P D2 A5 i( ?
- public String getModelName() {
" F6 q: Y% i, _9 Z! Y/ R0 G& T - return modelName;
$ P) p# Y1 x& n2 _ - }( Y; Y: K! E: }/ Y% O2 O7 e! [
- public void setModelName(String modelName) {, ^# O* s6 `0 c$ a" N- {' J
- this.modelName = modelName;+ x! q9 r$ w U% m6 Z/ Y/ s B
- }' y$ t# }, R3 C/ S* `" l
- public List<ResultField> getResultField() {
. Y% K( H4 h# t+ [' Z9 M+ I - return resultField;
% H5 S" ]: i. q( z( X$ y - }7 P j2 X: K8 o& a: h
- public void setResultField(List<ResultField> resultField) {# o- O! N- R; S0 @, e, W6 L+ N$ j
- this.resultField = resultField;
4 s d1 @( b( A9 K1 f - }
6 o3 s, T" K! T/ G - public String getFileName() {7 ~2 i- i: j1 o) v8 V
- return fileName;
$ y, g4 u& {! ^6 A - }
) f( ^ b1 v. d U - public void setFileName(String fileName) {) h; [. W9 X: n- j- y1 b8 C: W, {" p
- this.fileName = fileName;7 z) i' {, F, o/ b( r' _7 T
- }
# ?& B; H7 a/ m, ~ -
3 x, \9 y( H9 ` - }
复制代码 在转化的时候需要这样来写:/ t: v5 f$ _7 u7 B5 g& T
- Map<String, Class> classMap = new HashMap<String, Class>();# }4 q$ m0 s; w% q
- 7 m `! t; o( r. x- i- h/ d
- classMap.put("regularCondtionList", Condition.class);
( {+ {; C5 O7 a3 J - 2 g& d/ I v7 R9 o6 p8 |6 B' y
- classMap.put("dynamicConFieldList", ConditionField.class);
/ h) u# F0 e6 X" Y - : l( `$ f B) k- N
- classMap.put("resultField", ResultField.class);- ^% \4 J* l8 H
4 g' _7 \" b5 l' x* @+ P- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
. w" [' L7 [7 S8 U R( `2 \4 q0 T( S' T* X, q9 B$ s' {
% R5 f) @$ [5 i* v& M3 ~
' F- _2 \1 m$ @$ v% y
- b" U5 `. n( e; J& W! A+ E
|
|