TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
6 u# Q9 |) y8 ~- H6 b D但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。
" f; A5 U& ?2 {4 g$ f3 w解决方法:- public class Student implements java.io.Serializable{! T( O& f- A% p2 q' u# j: v/ `
- u9 {8 U, r1 X2 i$ N- /** % e# p; s1 B/ q, U) d
- * @Fields serialVersionUID : TODO
6 `8 E; Z# S H2 n' U7 }0 C - */ ' q! }& ~* ~& u0 \/ V& E* ~
- private static final long serialVersionUID = -1943961352036134112L;, o& f' v4 ?0 s5 [6 n
- private String sname;
: {7 y- f$ s0 V- G& p; Q& _# r+ S - private Integer age;
5 _ k5 d: [% q0 `0 X& [( x: l - public String getSname() {
$ p( P$ E4 f7 g" V+ R - return sname;
) |9 N5 {0 e6 i' g) O |$ o% h - }
# @' h( D* M) ^, D - public void setSname(String sname) {$ S6 N; X( y# v
- this.sname = sname;
! J9 l8 g% |/ G7 O& O - }* e3 U* L7 Q, W. H
- public Integer getAge() {
- W2 Z# u7 s' h - return age;$ |" A* G: |6 w' s8 ?* m* W$ s
- }
4 L7 O6 L! a7 N - public void setAge(Integer age) {8 S, W& i2 z+ E0 O, K
- this.age = age;
+ T) p5 W+ M: ~4 v& E. A - }& F: F/ I D+ d+ \* t
- , o$ `. z0 R4 @4 D" G
- }6 K1 n4 V: P" o7 p* Y9 W
复制代码 由字符串转java bean时可以使用以下方法:+ D( i7 A7 [6 w5 T2 U9 Z3 ], k/ D& l6 @
, x) s7 a g& t% L! X- JSONObject jobj = JSONObject.fromObject(str);
. A2 j! d) M- D0 ] - Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码
2 v5 t! q6 \" i7 V; F/ Q- r是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……
; I8 O2 I! m1 c( m 在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);
: Z! d5 N1 G. n; E' a7 m1 b; s8 X
' t! H6 N$ `$ L1 X比如我定义了如下类型:
* W* Q3 y( I6 ^: P- import java.util.List;
1 a+ ~1 n( w8 F$ a, S - /**
6 ^- X2 }4 U! P9 s - *
' U# o( | q2 ]5 F8 b/ v/ } - * <P>Tiltle: AnalysisModel.java </P>: l3 R( g; z- ^6 i1 a
- * <P>Description:模型类 </P>
4 r: b' u5 g$ m. E% Q- N# W - * @author biyutong
; m/ c e2 \; H) z - * @date Jun 11, 2012 9:37:54 AM - T# g B/ i5 B4 J. e
- * @version:V1.0' x7 t- c9 ^- H( r3 D
- */. Q, y9 B- _" n5 \5 a: Z
- public class AnalysisModel implements java.io.Serializable{1 t# s2 y- \1 c$ C7 w6 D
- ) R; ~$ z9 H" I
- /*** e% _3 q5 N8 v" E4 K# J. V( f- J
- * 固定条件
0 f M$ o: _6 ^+ M5 Y/ _) z1 p, A - */2 e0 ?- l) ^. l2 O/ L
- private List<Condition> regularCondtionList;//
4 _0 e; P1 A- ^, ~ - /**
- R- O& l, L3 F) Y' n - * 动态条件& M* G! a2 U: k' @. Y7 y
- */
/ ` y9 f9 ?$ q+ U6 a - private List<ConditionField> dynamicConFieldList;
; n3 l& S' \) E+ u9 M - /**
6 `6 P' f) l# W, c - * 结果
/ V4 Y5 P Z( R# R* i' M - */
: j, _' {$ H" X1 z" ] - private List<ResultField> resultField;
* u W( B" Q( c# H: M - /**
# w: e# O/ s" R s/ o* U1 @1 k - * 获取前多少条数据
( J) M. i' k2 |5 v4 w, J) B# k - */
) _5 o9 I& D5 n" T - private Integer topnum;
6 j$ `- P* ?- j" [4 T( } - /**, m% G% N- O, s& e
- * 顺序还是随机$ p( j# A( P* u
- */- F' I8 u" o6 W0 [
- private String sortFlag;: A7 ^% d; t" O! }
- /**0 j4 S9 ]8 Y2 t2 E2 l
- * 备注
2 b& R! K9 ]4 q" F8 u" f - */
2 v# K& ~- }! B: B - private String remark;
C( A' f2 S0 k) ]# ]' A - /**
# f' F" r1 m" Q& h8 A - * 模型名称/ R5 q6 O( I; ^0 `
- */* {8 a/ }2 v- T) X
- private String modelName;
6 E$ l2 L+ _7 N4 n% x: F - /**2 _* |8 ^) N# n1 e: N" Z
- * 对应的xml文件名称% O3 Q' `( j+ x4 }; e; I
- */& ?! U$ y" q2 O/ \0 i$ q% `5 W
- private String fileName;
8 F, o4 S: d q: D, s' e( \2 T - public List<Condition> getRegularCondtionList() {% Y5 G3 }" m( o: L
- return regularCondtionList;
4 y8 a+ l, }- K - }) O- [% m, Y$ A# W
- public void setRegularCondtionList(List<Condition> regularCondtionList) {
# \$ t5 c* d) d, E+ j9 ^ - this.regularCondtionList = regularCondtionList;" ^4 @# e2 m' K( K7 v$ Y r" d
- }) y0 K9 Q% e. K+ h
- public List<ConditionField> getDynamicConFieldList() {
! [. B2 J; ~ ]7 G( k - return dynamicConFieldList;
6 D* v1 v- f1 q/ N6 X - }6 L5 h- T+ x5 G3 U
- public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {4 j6 g; L" a& y/ V" l% z0 W
- this.dynamicConFieldList = dynamicConFieldList;, K% Q" h& }$ l& {" b
- }
$ H: ]+ J7 T# z% N0 r - public Integer getTopnum() {
1 s1 E* e# z1 Y) a: D, e% } - return topnum;
. a6 C1 [2 O3 _$ l: H - }
2 p. O5 }1 O9 o4 J% K2 Z - public void setTopnum(Integer topnum) {
# m& n' e/ W& l* C - this.topnum = topnum;
. {+ g! e- y% }# s$ F2 F - }
9 b" g8 ~4 u [" k% ? - public String getSortFlag() {5 y3 {: Q& q/ G5 C
- return sortFlag;( W9 K5 N e7 F8 ^% f4 B
- }5 D$ t- ]8 z* D; i% H
- public void setSortFlag(String sortFlag) {
7 }8 F! z7 ?! ~ - this.sortFlag = sortFlag;
( q1 t9 h1 n! s5 y& l' y - }
( Y @- ]& X7 ^) }- ~+ z - public String getRemark() {
4 G/ Z% H6 [4 ^ J - return remark;5 z6 n" p; E' I3 U8 [0 _' N& y
- }
0 w( @9 d7 m6 t. e: ` H+ A - public void setRemark(String remark) {7 Q- f7 ]8 [( }3 M
- this.remark = remark;0 j/ w3 u% J0 B% [% j! y
- }
3 E D4 A7 X% z: y8 D' c/ r
+ ?7 `1 [3 q5 R- & _, g* C" X" G( Q U+ U" Y1 [
- public String getModelName() {: ~) E% H4 h* v8 F
- return modelName;
8 B$ ~1 ~3 _( F2 U+ p - }# X/ f, |0 D; `# L- \& c# y% E$ A
- public void setModelName(String modelName) {) N+ I! H1 c7 I9 E' F$ Y# e* z6 D
- this.modelName = modelName;! g: u/ T/ T$ U
- }5 M0 E8 ]% G3 b5 V' R
- public List<ResultField> getResultField() {4 U! \5 I7 o2 g* T- N
- return resultField;/ e9 T( o( U5 S) f% s3 Z
- }3 H) R( \3 N: [+ i
- public void setResultField(List<ResultField> resultField) {+ u! K: f# {8 q' a1 w+ p
- this.resultField = resultField;; P2 v! h" N7 S+ y
- }7 _& O, x& a* m
- public String getFileName() {! }3 [; z2 l- E. z* v
- return fileName;% w8 {2 ^! m: j) l. b0 W/ E, ^
- }
! c: S" k; a5 m' ^& W; l - public void setFileName(String fileName) {" L: h5 N2 U3 P, V
- this.fileName = fileName;5 W w3 p" k4 i
- }/ f. o4 d, F. V- W0 [* f
- 1 Y6 Y& N' Y3 N- d9 b9 a* f
- }
复制代码 在转化的时候需要这样来写:* h" C; @; A' F) ~( u" J5 F
- Map<String, Class> classMap = new HashMap<String, Class>();) A h# C! d3 m, ^, R2 L. h
- " B% q2 l# f* |
- classMap.put("regularCondtionList", Condition.class);+ {5 i- S6 |9 `- A9 e
: O# } o+ b; _3 j- v- classMap.put("dynamicConFieldList", ConditionField.class);$ J, t8 W4 E& x4 t- p; Q
; L, b& _0 K" s# [$ U- classMap.put("resultField", ResultField.class);' C9 x) n2 \7 g
2 K. p5 | P* j+ U" F3 L3 |- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
5 N& D. J% k5 _, x( Z) o, W1 @; E
! n$ Z6 |# \9 s: p! t/ m
+ N8 m+ ]1 n. R% u+ g. h1 @( R! e: f! Q( T3 V+ V7 l
|
|