TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
" Y" C; n/ [) n6 ^9 X! Z6 o. p但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。
/ ]/ _) F5 B' M: [9 d解决方法:- public class Student implements java.io.Serializable{
4 d; C$ k4 L6 g! l1 ^/ _0 Q3 R% N - 1 m, J$ l; D. U6 U9 ~
- /**
% J+ K7 M E- T' z- d, ]1 J - * @Fields serialVersionUID : TODO! m: K) l' T* e9 ]6 J e
- */ ; E( ^' e' B* n4 b
- private static final long serialVersionUID = -1943961352036134112L;
5 C/ ?* G0 a ~2 l: i; ? - private String sname;5 p4 O ], s7 e* z7 ]6 ^ }4 n
- private Integer age;" l% I3 G8 I) J3 f4 U# _) `
- public String getSname() {' T7 W: p; e A8 z* z: i
- return sname;
+ c/ r' U9 z+ w* y% L - }9 ?' R5 A) J2 t& d ]. ^& j
- public void setSname(String sname) {1 n; \, C' K& F
- this.sname = sname;% p, K2 U9 {1 l7 V
- }; Q' _% M0 @, ]$ p2 ~
- public Integer getAge() {9 J& U$ H' b) A
- return age;- ?5 s i4 y9 q6 K
- }
8 j6 E3 z: g" q8 I& T* ?& x6 H - public void setAge(Integer age) {
: _5 N, D3 A% |$ m; m# d5 T3 i - this.age = age;: Z7 `/ {2 Z% u0 w/ I) H, O
- }5 G# ?) u! g) b; j2 c, W" c
-
3 q) h# h4 i* C# f3 B4 a - }
5 U$ a! q! `" j- Y4 b
复制代码 由字符串转java bean时可以使用以下方法:
4 b: {' n$ j( c& K5 M: J. ]3 U( l; ^6 | i3 r
- JSONObject jobj = JSONObject.fromObject(str);
! l6 T& ~7 K, r$ s: Z - Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码 6 Z t7 W$ {) E; v, Y# p
是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……5 Z* P( A5 z. Q
在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);
* Y( ?& E6 Z3 Y" O# r' J3 t f3 s/ g3 w! m
比如我定义了如下类型:
e) u1 o2 ~# r! E) `7 d) u- import java.util.List;- M$ I$ @/ Y% M2 E8 V& P9 L
- /**
' Q' |7 X$ O/ s" N4 J - * 6 Y$ Z) _" P! h6 Q
- * <P>Tiltle: AnalysisModel.java </P>
, E* _! r/ y, Y. @ - * <P>Description:模型类 </P>
6 k$ Q; X& m- k$ S* i - * @author biyutong
$ b$ u2 K8 S I% o7 g7 {1 p - * @date Jun 11, 2012 9:37:54 AM ( n' x. @; a) E6 H( I
- * @version:V1.06 r$ _7 e+ N5 [5 B, r9 `
- */
7 C S1 _- S1 E! \1 D8 }, w( N - public class AnalysisModel implements java.io.Serializable{
. m4 U$ M6 s( ^: D! Y- z6 U H4 p
( i6 h. A* e3 `/ V4 N+ q! \( D- /**) g5 G6 ]$ _/ V0 W& T ]4 l8 \6 x
- * 固定条件6 _( h5 K2 y, w3 W' E
- */
& K4 k6 }0 [: J7 y6 s; f - private List<Condition> regularCondtionList;//8 P0 }- \8 k3 X* C! B' R* ? t2 `
- /**
- u* C5 ~/ [9 q4 q+ {, r - * 动态条件
i! a: A8 U/ g% i# o - */
~7 |: q7 p/ \8 r& o2 P; C$ w+ J - private List<ConditionField> dynamicConFieldList;
. B' ?$ f/ @9 R, E. L W* c - /**6 Z5 J( S+ j4 } C7 i. N* {/ A7 {
- * 结果 j2 c, ]( G- {' S5 D
- */
- |! V6 t: Y: A$ O) @ - private List<ResultField> resultField;
1 E% E$ ?2 v7 @ K d4 a$ R - /**
0 w! I0 n) w- F2 k5 J - * 获取前多少条数据/ n/ W4 h3 _' z+ K9 e1 p2 n, |, z
- */$ D3 [' m+ L2 R) Z
- private Integer topnum;
! X. N. Q0 v) p! W7 E* K$ f/ i - /**
1 m* l/ G: F# |# _0 O7 |1 r - * 顺序还是随机
- ?. s. Q* y' D* u4 X6 V - */0 x; g: ^3 }2 Q, h
- private String sortFlag;
' o( o( C/ D4 { - /**; G, Y. [# n3 p$ B- x" q1 D- E; q
- * 备注
* S% |+ j$ A& ~6 n7 I [ - */
9 Y# @$ r3 d, N' \& S* U5 Q2 g- } - private String remark;
h4 }8 r. m' R! P$ B, W( |; N - /**0 J8 z! U' I/ ^
- * 模型名称
# ~4 W2 _5 K5 L! m, y4 g2 B+ s4 ]; S - */
0 J& h9 N2 [8 H - private String modelName;; V4 g0 G0 j9 O4 x) I3 L
- /**9 W6 l( V5 i/ k: s# w: R
- * 对应的xml文件名称
& P" ~! B/ \. v; G" {# f - */# O" h3 k3 Z8 V% J
- private String fileName;" a( @; u1 G; I
- public List<Condition> getRegularCondtionList() {9 i! v& j' D5 o* B
- return regularCondtionList;
5 r5 r. z* p8 o/ ^& o - }
+ ^1 }" L1 K4 F4 h5 @: q - public void setRegularCondtionList(List<Condition> regularCondtionList) {
- T% B3 w7 q6 [ - this.regularCondtionList = regularCondtionList;- m5 k3 y$ i: L# g/ H, K; }5 p
- }1 H* T( i" R5 O
- public List<ConditionField> getDynamicConFieldList() {
' b. {, _8 D& d, E5 r& [3 o - return dynamicConFieldList;
' e1 x2 V) Q1 n0 H/ W9 D - }# \4 x. b- \6 f
- public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {- |6 u# G7 [9 @0 q% z% p9 H) y
- this.dynamicConFieldList = dynamicConFieldList;
7 o6 G2 @# j+ P7 W$ }6 k6 |1 T - }0 ~! j8 y# ~/ w: K# s
- public Integer getTopnum() {! k: X+ D1 v9 Q/ y- ~/ j
- return topnum;
/ @% `& Y) p# _& g X1 m; Z( R - }8 e2 g, m. c$ M) k
- public void setTopnum(Integer topnum) {
' @! d0 ]8 @4 @, d& n1 P! U/ {* { - this.topnum = topnum;
: J9 E$ u4 T) H+ p - }- t6 l8 G5 A9 j
- public String getSortFlag() {! @8 c3 |( f% |+ S
- return sortFlag;# O) R' c& K, k* [7 s/ [
- } o' E9 Z8 X' l0 K9 S- f1 g: p1 C
- public void setSortFlag(String sortFlag) {7 }2 O# U R3 _+ b/ a
- this.sortFlag = sortFlag;
* N& @; s8 d; s. }' e7 S9 a - }+ r; L) D* ^, s6 b% o2 T, G1 D
- public String getRemark() {
) \4 Z5 u0 n" W% b - return remark;1 M4 v! ~4 a) }% V+ }: C2 S
- }
* H$ I! t! R5 X0 N- } - public void setRemark(String remark) {% U$ I! I, x8 f4 e: F8 F
- this.remark = remark;
d1 h# Y& N; b+ i: D - }
5 G) y. l7 ^2 \+ F; b3 B/ P
6 a1 ?" T$ j% x+ M$ R( E( d-
/ K. I1 ?1 ?+ S+ G- [/ y- c m - public String getModelName() {5 f8 I. x% R# n, C1 o l
- return modelName;- K' g7 U+ H/ a& f0 W0 N
- }
% m! m( A0 o E - public void setModelName(String modelName) {
+ L# I9 |- n' i5 p% `" `1 m - this.modelName = modelName;5 S4 H# x1 Y5 o/ e2 w: g
- }: u$ j6 S4 c; K* J4 s4 P, x
- public List<ResultField> getResultField() {. n- U! s6 z" r1 E& N/ j) z
- return resultField;
4 Y. K" A0 r q - }
" P/ Y* R, O. ~ - public void setResultField(List<ResultField> resultField) {
5 {9 B9 m! w: J& W - this.resultField = resultField; O/ p; U5 X% q; g2 x
- }( Q. _7 M8 } {4 o
- public String getFileName() {' g1 n I( Z) |3 C) `8 Y
- return fileName;2 L: j5 z' G; j0 c& b
- }
; U2 V ?% {5 ^; H - public void setFileName(String fileName) {
+ l, y$ ~4 y& B3 l( \ - this.fileName = fileName;4 R0 X9 u* [8 h: d
- }4 N6 T, l, [+ `( x
- $ l$ ?) R/ ` L' ]3 I
- }
复制代码 在转化的时候需要这样来写:
$ r4 C$ V# P' J- ?- Map<String, Class> classMap = new HashMap<String, Class>();" T* D2 `. r! Z+ Q
) P% K& A" B& J) A8 P* x- classMap.put("regularCondtionList", Condition.class); d1 L' P6 m+ w. d7 [1 j
" Q- J7 i& L. s$ ?. R6 a: K- classMap.put("dynamicConFieldList", ConditionField.class);
' F+ p+ C Q* M5 R
. e$ [* I, f! |! v& Z- classMap.put("resultField", ResultField.class);
+ z6 v9 G- Z! i - 5 h8 y8 u! S4 v p' q
- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
3 l7 M0 \1 M4 b8 A
`- q- ?2 Q& b
" _. \; c" m4 f s3 W0 i; m
( F5 e7 A! N+ D j. L1 h
4 X' F! I: q5 q* c! o/ t, g |
|