TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等" @: d2 c5 Q( y, k8 x, s1 p* b
但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。3 _. K5 g3 L7 Q
解决方法:- public class Student implements java.io.Serializable{* h9 g/ Q' T$ L" k: D' T0 w
- 5 b- X6 c5 S8 B$ @/ }
- /**
' X2 X* \ Y( j5 F$ Z) u) J - * @Fields serialVersionUID : TODO
7 W5 g' m1 x) c2 S. L - */ . T5 f; |, s3 Q" M1 T# w. U
- private static final long serialVersionUID = -1943961352036134112L;
. p. f$ g% A/ T0 J1 E - private String sname;
8 [& o6 p6 k! g6 u$ s - private Integer age;
- m5 g$ ^* a! t& V" O - public String getSname() {$ ?; p( j0 `. h, p. _3 ]7 f
- return sname;9 W J0 F& ?, B9 a* d
- }" g, |% t- W5 K( Z6 a
- public void setSname(String sname) {
( e7 A1 J6 M0 w L+ ~7 f/ ~ - this.sname = sname;) N5 \8 e+ V5 o/ L3 X5 \+ k, `
- }
$ S# }7 \ G+ }8 n- |/ I# v+ h6 a - public Integer getAge() {
( m" S5 h2 T$ g8 @4 R9 F - return age; e0 D8 t# L- c j& f) T. _9 P- E
- }3 M0 g1 j' F0 Q/ N7 _* e3 I: J
- public void setAge(Integer age) {
7 h7 `6 Y% a5 z U9 I2 v - this.age = age;
6 f. ?& A$ L, d' ^$ ^ - }* h! {! R8 w, O0 T
-
; X- M1 o1 g$ U+ o' w7 `0 j8 `. I - }' S9 H6 L5 T& m- i Q8 y! o
复制代码 由字符串转java bean时可以使用以下方法:+ ?- u1 W) r/ G
! |# `/ z9 ^) a1 l) p
- JSONObject jobj = JSONObject.fromObject(str);
" i3 |7 O, X. F9 b) W- [* V - Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码
+ G: M* g% @1 V' V是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……
6 @0 M k6 Q, N; ?3 ?& v- s3 l 在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);
3 _& g y* i1 ^/ o% z
) _4 O6 G4 f) c$ t2 x. h比如我定义了如下类型: G$ p: n/ D: v4 z+ H
- import java.util.List;
1 [( R7 p, l7 @% i - /**
+ p6 r$ L$ ~) `& d- S7 Y+ R* D) h - * ) n! x; Y* z; J+ t+ X% T- S
- * <P>Tiltle: AnalysisModel.java </P>
: G) }/ A& o3 X1 \6 y0 X `9 ]- o - * <P>Description:模型类 </P>$ Y L+ f5 m; |/ b1 R3 B
- * @author biyutong) J( v( N$ B; `% V3 |- z
- * @date Jun 11, 2012 9:37:54 AM
( m7 ]; R. r6 G8 @ - * @version:V1.0" w' s8 ^1 V- a- j# S/ H
- */2 H5 `# f: e- `! | q9 O2 T
- public class AnalysisModel implements java.io.Serializable{ C& \; t& z; h5 i; v
- % S% C+ f% d+ a
- /**
& X; n6 M$ P+ W# W# E3 H$ m8 u - * 固定条件
# Q' X0 f" V% T- n7 S& x" g - */
8 L# d% |1 y+ p# z$ G6 n5 d. U - private List<Condition> regularCondtionList;//, ^. W) C1 a" u! r- n/ \1 q- v
- /**
0 J- s2 P/ e0 o7 M6 a9 H$ w9 g3 ` a - * 动态条件
3 k: I, [- k4 u+ i - */
. g/ k' f1 ?) `7 L1 y+ @ - private List<ConditionField> dynamicConFieldList;
6 x3 ^. m2 o" e$ P y! W3 ~ - /**
8 H. R; x9 ?4 O - * 结果
3 P% Y4 T7 [& K, U/ G0 q6 D - */, Q* |( }7 Q- E- O5 o, h g0 ]3 ?
- private List<ResultField> resultField;
/ U; ^9 ?% `& L, R! X* s - /**! a* _6 I& j) x1 f/ k+ j
- * 获取前多少条数据; M, q+ N" [1 A5 k
- */
d) U- b2 Q2 t0 A8 T - private Integer topnum;7 m% }7 D2 f! e$ t
- /**
; X! C2 F! V% M' b; ~1 F: O6 f1 l - * 顺序还是随机 Y6 {, S& Q0 D% _
- */7 a! h+ l9 ]7 t- s
- private String sortFlag;$ ^7 K8 @% I8 \8 O! T2 v
- /**2 q6 r. J2 m* F: @1 \& R
- * 备注
' R' ?! U7 j% h5 o - */6 @- D3 e4 o3 S+ H8 @
- private String remark;& ^% k* f* I/ @. S, f: m+ _
- /**: O/ V9 U% P4 _' |0 p
- * 模型名称# } _. Q0 w5 Z7 b
- */
0 ~3 y5 U- R* f z9 |: O - private String modelName;# U' l. u6 p3 s# s: G5 x
- /**( h4 V) j" L( d$ F, y- B
- * 对应的xml文件名称
, Z' I9 a7 j |. G( \9 ~% x - */
2 \8 h D. y5 r* N3 x - private String fileName;$ q8 T& T4 x& l
- public List<Condition> getRegularCondtionList() {" P" R1 K1 k% E) ?9 V
- return regularCondtionList;0 a2 Q; w j" d$ k9 Q }2 j
- }
" R4 s6 X8 ^. v& k0 p% Z0 \8 u5 ] - public void setRegularCondtionList(List<Condition> regularCondtionList) {
7 s9 \. k: P% n m, x - this.regularCondtionList = regularCondtionList;1 K( \# ~ H/ `' `1 b
- }) b8 y/ M; y& [4 T3 U: h, E. P
- public List<ConditionField> getDynamicConFieldList() {
, O+ r8 f" f: Q" R" o: H' Y0 I& X - return dynamicConFieldList;
' c* _& e& P( z' t& [( a - }
" s" b: k8 g* L# v6 }- y% n - public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {7 n6 ]% V, K) _3 o' E
- this.dynamicConFieldList = dynamicConFieldList;
% x; R. y! g7 }8 T" P, E - }
3 r7 l4 _/ K" M6 p, h6 W - public Integer getTopnum() {
2 o' q" y! b3 }. N( R - return topnum;
, p4 v* H$ ?4 `& u4 ?$ Q - }
7 H: s* p6 E. C7 ] - public void setTopnum(Integer topnum) {
6 f7 T, N/ I9 o, ?' d1 H8 j9 c9 | - this.topnum = topnum;
2 O9 v3 X# {$ o$ F! L - }
: I2 ^7 e9 n0 W* @% U' f - public String getSortFlag() {0 \! ]( }* W" A7 l5 X6 {; H
- return sortFlag;
: V: W3 D! m* g0 \4 H4 t4 P - }
G4 J B5 ~# J- N! B+ ~ - public void setSortFlag(String sortFlag) {# x: V8 x! A* o, z7 y4 C t+ T3 @
- this.sortFlag = sortFlag; U7 P8 k* C& S6 M+ R
- } B) }- I+ C3 f! H ^
- public String getRemark() {, j4 W+ J# H; N+ T7 o( F2 f
- return remark;# }/ n6 l3 Q2 b, ]8 O1 S
- }
: q/ |6 U. k3 T7 a7 N( g4 e - public void setRemark(String remark) {/ m- d, P; B( Q# c/ [$ J
- this.remark = remark;
m6 p2 ^3 }6 Y' P( N# D$ J - }5 w; c* A. p- h/ k
* E3 U! R, t/ `, Y7 g7 O- + \: ?9 F# p9 W) d1 x. I
- public String getModelName() {1 k/ [' J' \7 d! f3 m
- return modelName;! x9 \% ]( U, G% ^$ P' O4 J
- }
+ s5 a3 w+ z! Y. O4 i) ]. A - public void setModelName(String modelName) {+ K9 M0 q; u( K- B# A8 M' R
- this.modelName = modelName;- m7 s: g1 i |1 r% r
- }
1 d. |0 t4 x+ I: | r' T - public List<ResultField> getResultField() {
! U; ?; T: F4 X" W7 t' X/ q5 A - return resultField;. C2 g6 U6 I3 n
- }; x# q7 M; J: ~# v' p* P( ?
- public void setResultField(List<ResultField> resultField) {
5 ]0 ]5 L2 c$ j5 k% @1 j9 U- v - this.resultField = resultField;% w# A1 c, U+ p. u6 V: Y
- }8 B+ S1 L0 i! U: F
- public String getFileName() {
1 z9 {& X0 p1 N1 }: w% V+ ]6 U - return fileName;* [- P/ `( p' I( P D) V& h: H
- }
* l* ]3 d9 ^4 y; ^1 c+ ]/ C; l - public void setFileName(String fileName) {
f+ K B1 v. I$ o. D$ n, a0 Z @ - this.fileName = fileName;
% ~5 Z) m- s# M% b% w' D - }
4 A- w4 E) A+ A8 r. v - % N6 X3 F5 ]+ Q7 o) Y
- }
复制代码 在转化的时候需要这样来写:
5 l* C! m; N0 k% r# l- Map<String, Class> classMap = new HashMap<String, Class>();
) q2 s& Y8 P. N0 I
9 Q& u, ^$ q1 F2 l A& [/ C- classMap.put("regularCondtionList", Condition.class);# c& w- Q" I% a6 [
- / g5 v: |# O3 w3 C$ I- J# Q3 T
- classMap.put("dynamicConFieldList", ConditionField.class);
! R1 E1 B$ U( p
' _' o% S2 e9 I8 \. a$ w. |- classMap.put("resultField", ResultField.class);2 d' ^1 U' V! X9 ^" k) Z: o
& i. h% O+ @6 t- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
5 U' k% k- @' C/ n* n$ @/ M2 H' u' {6 T3 Y
5 ?0 y: b; \5 H/ w
) F2 v1 i. A$ c7 s8 l
/ V- F/ w' p. x! ^' ? |
|