TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
2 r3 \8 J- s( X5 n: ]: O7 K但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。 p/ x0 l$ J0 Y5 u+ w6 \8 Z( n
解决方法:- public class Student implements java.io.Serializable{4 n6 F+ I7 X8 p6 T: T8 U5 |1 R
- 2 S9 e5 ]: H, }3 G- o% N1 |. ~
- /** ; O& `' ?2 {( e. V7 z) V2 Q/ N
- * @Fields serialVersionUID : TODO2 O. c9 ~+ k0 c* \; e) w
- */
3 i; A6 t, ^" c0 F. C - private static final long serialVersionUID = -1943961352036134112L;
4 r6 Q+ j2 [4 p, `5 e* @% R - private String sname;% @' E! i3 Q& [: m: j) [- a
- private Integer age;
: x% Q/ t* e t! v - public String getSname() {
+ c9 O$ P$ I. I$ f. }* [; q. U - return sname;
. u: W- H6 L+ |" ?6 Y, [6 C. ] - }
) p9 C* ?: V$ t* J5 A8 |6 J - public void setSname(String sname) {
. r& J \/ C: \7 S" M - this.sname = sname;- y1 P+ }. ^1 {5 K, l5 K, L
- }. E" v5 A# J: D/ P2 k( D. H
- public Integer getAge() {
+ W2 }0 O$ m; q3 k# y5 T - return age;
# h! Y' r5 k5 k; A4 m - }
4 ~: W }9 R& Y. G - public void setAge(Integer age) {& p f3 _' ~5 B5 I. L
- this.age = age;2 e7 i7 B7 U' b8 j! \6 E, q5 B
- }1 x% r; s/ t( ]- V' {
- $ | @3 `9 P; ]& d. j! l
- }" V/ J# ~$ _, |0 m$ x% g! F
复制代码 由字符串转java bean时可以使用以下方法:
5 z% O- J% @* L9 i
* v$ D% T2 x: f, Z8 |4 @1 K! M4 ~' b- JSONObject jobj = JSONObject.fromObject(str);
5 ~/ G: S7 u5 U - Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码 5 `* B7 |! Y% U0 J1 J
是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……2 w) |9 D0 k" x5 G$ D
在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);% v( U: g1 c" }1 d$ S
9 i' D( h! t/ Z- D8 E- f O比如我定义了如下类型:
8 T! i! ]4 {/ {/ S2 T, l- import java.util.List;
1 D# B7 @3 |$ x: n - /**
. |& o- l+ u8 j- I- \- C0 c - * 9 W) J3 r$ `* x! z% s6 x) n' m4 f
- * <P>Tiltle: AnalysisModel.java </P>! W, Y! W) P9 B
- * <P>Description:模型类 </P>
1 _9 [# O' \/ P& L. k: I; x; \" v - * @author biyutong
5 a1 i: b t5 v- T8 x. V - * @date Jun 11, 2012 9:37:54 AM
: F8 G' j# m! [7 Y9 j4 y+ K - * @version:V1.0; v; [( A0 C$ F5 |2 W! O
- */
8 c2 C4 m( v" k F. G* I7 n - public class AnalysisModel implements java.io.Serializable{
3 a* |0 }6 s5 b4 ]2 R - ; V% U" \. K2 E' h" b
- /**
7 R6 [+ Y' y6 p7 `. H - * 固定条件. B% N8 R9 F k! A+ L
- */6 G( X8 @8 @* u' W& C/ I* V
- private List<Condition> regularCondtionList;//1 U' Q" t/ u' {( Y
- /**
3 b, X8 E& U# Z; A! [& K - * 动态条件
4 U( c7 ]# f) Z3 q- h - */
% j* {; j9 }9 O9 K1 K - private List<ConditionField> dynamicConFieldList;
3 v0 R: ~' x, w( m) { - /**& q) k8 h2 e- E1 G
- * 结果
% `) c. I( `' F5 G2 G - */+ O# w# b# r( j! O. `3 j* s
- private List<ResultField> resultField;% v V- f* A/ X9 v% |4 a
- /**
3 q6 L5 C8 z8 I5 V+ M. C3 j3 ` - * 获取前多少条数据1 G2 Q- P; i7 A b% a$ Y- ?1 }7 x
- */8 H+ `6 \- \4 y) W3 h6 X. m
- private Integer topnum;
* v2 e8 Q; C. U7 n" ]' {, B5 @2 c5 w - /**8 |; t- j4 M& ]! q) J5 I s2 I$ B
- * 顺序还是随机
7 h( C- R: U8 k3 f1 B! o - */
: h8 [0 P) T3 a8 P - private String sortFlag;
0 h- Q* m7 a2 ~% [9 C, T4 J - /**& E8 i. @8 B5 o- c
- * 备注
; u2 `) D1 G6 H. i( \ - */
4 b. a& |9 [2 k$ f - private String remark;
5 j0 c6 W3 F; U5 f5 T - /** D! K# |# i: V, G. {, V
- * 模型名称
9 p! k% ^7 u: J+ S& H8 D; S - */
9 R ~( L1 y( O- k d3 ?6 D& S - private String modelName;
4 R$ L2 G# ^. L7 L1 t" K- F - /**$ ?! N7 U$ r5 a9 s! o
- * 对应的xml文件名称
l0 E: Q7 X" d5 |7 Q - */
9 V' [* |9 F' J% G, ? - private String fileName;3 E7 X4 B( c9 G) ?
- public List<Condition> getRegularCondtionList() {" }4 C1 B& n3 x; M% k9 Y/ C6 J: ^
- return regularCondtionList;
- y) y) ?4 ?. N& n8 H, | - }: X4 D+ z8 J% ?. H( n
- public void setRegularCondtionList(List<Condition> regularCondtionList) {
4 f$ o5 j! |4 j$ f$ O" { - this.regularCondtionList = regularCondtionList;, [3 [% I1 B9 b5 z$ i) b
- }0 M, h/ t; ?( R( Z L
- public List<ConditionField> getDynamicConFieldList() {
7 `3 v5 n# ]7 _' v8 A7 [; v - return dynamicConFieldList;
' f( p3 a! U% d" ?* t0 _" K2 m4 z - }, \) U$ j; A% ]: Q; P. [5 b8 k
- public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {+ U3 c- Y) h* U# y
- this.dynamicConFieldList = dynamicConFieldList;
0 @* ?! t* {2 e& }2 B - }5 o# z5 f' q3 F! z4 X- x$ m
- public Integer getTopnum() {; p% S \7 W( o
- return topnum; a0 @3 m' Q1 t* W. g6 T
- }" x9 K' R! _4 \. z8 r
- public void setTopnum(Integer topnum) {
& A6 ~) J/ N# v$ D& M q - this.topnum = topnum;/ E4 z: J# s1 ?
- }
9 a) z8 _& B7 ?4 ~ - public String getSortFlag() {* ], u( H* P& \! G6 q; F8 [3 _* z
- return sortFlag;7 C! y' j+ Z0 B$ u; d. }2 h- Y8 n1 M
- }
* U: `9 I, W% f, v$ U - public void setSortFlag(String sortFlag) {& o; W% p1 F. y8 i
- this.sortFlag = sortFlag;
. }) w( L6 Y5 l0 D( \ - }+ l ~: W# Q7 c, I6 a3 p u* Q
- public String getRemark() {+ \: W) ?7 J8 d
- return remark;
7 p7 S% M; p; l/ U( N - }& H: e9 `: j; t9 E4 _* P
- public void setRemark(String remark) {
! ^5 Q: A# `; V. F0 }: z2 z2 r - this.remark = remark;
, T( S$ @8 T5 A6 J - }: K9 ]( B1 e h' L4 M9 k
. N' D- R+ r9 r7 l-
- r: {+ }, T- l2 q4 @9 M# A/ C3 ~ - public String getModelName() {' B: u) j$ H! h, o+ Y
- return modelName;8 n9 F% A3 r3 n: C! @1 S; ^8 Z
- }2 [! u! r9 j3 v, A4 ]2 L8 X6 H9 M
- public void setModelName(String modelName) {
2 F M6 X6 O' b - this.modelName = modelName;& k- Y( t/ \8 Q
- }
- w7 B: M( C2 ]4 s: D$ g - public List<ResultField> getResultField() {$ p- V' x6 e) }1 i8 }
- return resultField;
9 A7 Y# k; |* c1 P* C) {4 q- O - }. _$ K" r s9 Q" I
- public void setResultField(List<ResultField> resultField) {/ L: b( g R" t9 I! i2 D/ _
- this.resultField = resultField;
# y1 r ~" b M1 s$ g9 o - }+ J u4 s8 M" ^" P8 q
- public String getFileName() {. v3 V: G. y! |; H; m( v' w; z8 K
- return fileName;
1 J) z; ~1 c8 p+ ?$ j* x7 q - }" ~9 I2 R, f% }) Y+ V, x0 L, S& ?+ N
- public void setFileName(String fileName) {0 Z- i8 P# Q3 C( s
- this.fileName = fileName;
4 v4 q9 N4 J6 `. C - }
$ L; K4 X5 c! i& r -
' Y. _4 o! C- i/ N - }
复制代码 在转化的时候需要这样来写:
; c4 I/ @/ \) ]' G3 V; l- Map<String, Class> classMap = new HashMap<String, Class>();
$ T8 E6 c4 j1 z2 c* A2 ]1 i1 W% S
. Q& q. N0 t5 G, h. S! b& o$ u- classMap.put("regularCondtionList", Condition.class);
M# Z' @9 F% N# i9 A* T
! p% h# P* M8 _6 |: W- classMap.put("dynamicConFieldList", ConditionField.class);
' W4 ^3 G! b- \ \- P+ B - ' @0 S& \; E- T- [- Q! A
- classMap.put("resultField", ResultField.class);
9 d/ d6 W1 I' f$ n* b) s# M
9 A9 ?2 i. Z/ ~/ d' R, K) o3 Z- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
7 \# q5 o5 a6 U: j2 ?; Q) l6 t9 \; y e8 ?7 P$ ^+ u
0 p2 {* x+ }7 h
! E+ v' n% P% I$ m' I
% u& Z, n4 D2 c5 p; V1 A
|
|