TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等
/ K4 g8 Q! Y% K$ j但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。
8 q5 O; B* Y9 x解决方法:- public class Student implements java.io.Serializable{
1 }9 G& B7 ?+ u& D( F7 ~6 {
2 ~ \7 e7 H5 C0 f1 X8 Z4 R. W- /**
0 u& \. }) B1 b% U2 y) l# k - * @Fields serialVersionUID : TODO
* Z: e+ A1 U1 s- u" m U$ y3 L - */ " u7 w- n% k0 n+ W5 A
- private static final long serialVersionUID = -1943961352036134112L;8 A: ?$ b9 k& A% q' J* f$ n) J2 j
- private String sname;
1 X4 @% v8 {) i6 ^3 T4 G - private Integer age;
# a4 f0 F. W2 O - public String getSname() {
; b$ H8 m% g- ]& r- S p1 y" ~ - return sname;
8 x/ {' ~0 b" C1 D0 l - }
2 W( F* a3 R. H7 W$ o* K; b4 l - public void setSname(String sname) { A# h& \$ ?/ }4 _. l9 F4 r
- this.sname = sname;
2 t! o" I, `4 @& g# e - }/ ^+ k0 f& X! M
- public Integer getAge() {: ^, j5 w/ o' n2 n/ y# l r
- return age;8 x' q$ |8 o, P
- }
# ~4 @& z# W# m - public void setAge(Integer age) {" G+ U- P, m3 N6 z2 M
- this.age = age;3 C/ m9 y1 ?+ J. W
- }2 M' l, u1 u0 E
-
" B5 a+ J; n1 N( ?' N; c - }2 s: b1 v. T7 A1 `
复制代码 由字符串转java bean时可以使用以下方法:
& t- H; I; Y( V' r
: r) S0 B8 C% G8 G; I9 I5 S2 C- JSONObject jobj = JSONObject.fromObject(str);# ?, L2 {- d% [3 n0 P: @
- Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码 . i; S8 M4 v$ }, Q F, v
是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……# I3 ` z5 H! o- g, {$ x7 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);6 r: B# {. r! X u6 F! H
, C/ A$ m1 Q* a2 _
比如我定义了如下类型:! i/ p+ F! ~2 t0 g( ]4 w2 c
- import java.util.List;1 T$ q4 {# e5 {
- /**
% \! g) X0 |( a - *
! R/ w4 q5 D5 E$ v - * <P>Tiltle: AnalysisModel.java </P>
2 P$ \4 i& m7 X% ~! t g7 v2 L - * <P>Description:模型类 </P>
' S8 [# u" h7 z - * @author biyutong
) ^6 @! R+ p4 w9 o, i- O) g! Z. q6 F - * @date Jun 11, 2012 9:37:54 AM
3 H% Z, J# k2 [6 O9 n4 Y$ t g - * @version:V1.0
2 p1 E* }/ T9 N( K2 c) a - */
1 d9 J' A1 z" A& z. j - public class AnalysisModel implements java.io.Serializable{$ [" ]( k$ q0 q p; p% k: L4 Q% k
- & l$ P3 s' a1 s3 Y
- /**
: p6 |" R0 s0 B% Q H5 K2 `3 f - * 固定条件! @: z, G6 G' r; t: I8 Q) H0 b9 }" ]
- */' L: t0 I8 u0 r- T3 Y9 o0 S
- private List<Condition> regularCondtionList;//# T4 K. Z" A! _% u$ |* `; v0 l7 E
- /**
! Z5 B% p D7 d$ v. f8 E2 \1 J - * 动态条件
' E! U: y8 i( N# g& J6 `/ m - */9 T0 N5 K3 g8 S' I& [7 E0 r
- private List<ConditionField> dynamicConFieldList;
' L! S- F' w5 J8 `5 P - /**
1 ^( \, G A- T - * 结果! n, b0 E+ o. M8 D* U& N
- */
) {/ _9 W* X/ X - private List<ResultField> resultField;
% ]: K! w# A% `) g - /**
( |4 ~5 l1 d6 z' g - * 获取前多少条数据% k7 C; H4 `( K0 z% ?( Y: p Y
- */5 g) h: N: i- u9 {
- private Integer topnum;
9 t7 u7 r6 V0 B: Q - /**0 Y S7 h$ @% `+ _' a# [) n; A9 r
- * 顺序还是随机# O% F. t3 w7 N1 L- U
- */6 A( h2 E: J3 E" e
- private String sortFlag;+ b0 Q; k+ C4 w. _8 t
- /**" t, `5 Y5 y0 d" U; m
- * 备注
1 t1 {( _4 B4 i$ S6 { - */) f6 D# U/ d! ]) s2 P% B
- private String remark;: T6 s. l, Q3 ~( v0 x" X6 b* {5 E
- /**6 M8 x. A8 w$ V: ^( p9 J
- * 模型名称6 P# j) h2 S9 ]0 H9 A2 s) Q
- */" {5 R5 E1 k5 w' A* v
- private String modelName;
5 h3 v% o& D: A" M9 K - /**1 }8 ?' S2 g- Q6 C9 |
- * 对应的xml文件名称
8 |, d& S+ R# w- G7 `" j, b - */
( u" L$ ~; V7 y! w8 V - private String fileName;
% ^9 {- e" Q3 B& F- z/ z( x5 c - public List<Condition> getRegularCondtionList() {9 r2 p: u# _( `
- return regularCondtionList;
' S, H7 i+ q9 M/ e- E4 R+ Y - }8 S: s( W+ \- L. `1 b5 f; {
- public void setRegularCondtionList(List<Condition> regularCondtionList) {
s- Q& ^: W. {' N+ v7 g& o - this.regularCondtionList = regularCondtionList;+ b4 V& V6 r0 f2 l3 B# \0 t- p
- }
9 _! i2 K5 o% l; g; @ - public List<ConditionField> getDynamicConFieldList() {
; ~' K6 E" k* Y( @- ] b8 J0 U9 U - return dynamicConFieldList;/ }# M/ Y5 v6 g+ t( M' V
- }
4 \9 K3 T4 s9 q0 e& i) ~! f" L# S - public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {
3 R6 @2 T2 J6 s! G2 k& X/ |5 C - this.dynamicConFieldList = dynamicConFieldList;& e4 D# h) T1 K% e2 `$ C
- }
. z: A# j' n% S - public Integer getTopnum() {
* `* ~7 |& ]( |3 i# m* } - return topnum;
% g0 }! F) @* Y% g2 r2 J/ C - }
4 `9 I& ^ ]3 r( s! Z - public void setTopnum(Integer topnum) {# G& r4 D; U" T; h* ?
- this.topnum = topnum;% [8 l; |$ S* U0 [, m* f9 H
- }! z! S6 G: E- n! d' M
- public String getSortFlag() {1 \# M- C( l) @
- return sortFlag;
2 }1 T$ x% _* B/ U# G - }
$ w0 g3 U8 `, H& j5 ]3 G - public void setSortFlag(String sortFlag) {( U( ?+ n' p# A5 S+ ]% w) I# o
- this.sortFlag = sortFlag;
, w% d5 g |1 P1 @- w# u - }
2 i# L9 f$ l6 w3 n) _ - public String getRemark() {4 G# \5 F$ N5 c2 W) V$ Y, B. f
- return remark;
, t, s# j% K% C! _ - }! n1 ~% p/ z2 h. N& [& b' q. M
- public void setRemark(String remark) {
/ O1 C9 O a) r/ _ - this.remark = remark;# C. u5 R: D- C) X( ^# x$ k- ?
- }, O! `! P. k; K- X& Y Y+ Q
$ `# I, Y/ t- @2 W# w1 A6 V, ?" Y& M- 1 x+ k5 S1 P# e( w3 u; b9 Z
- public String getModelName() {
, r& F8 @+ P0 V- @ - return modelName;) \8 k6 v& w0 S8 i% W' `) h
- }
* e3 \: s8 j6 J! g - public void setModelName(String modelName) {
7 V2 i4 g% Z/ v. ]7 d+ \ - this.modelName = modelName;9 |3 ?* @5 [) g- j" w$ |3 L
- }
9 r/ w, ^1 h* \5 \, ~6 K9 Q1 M& z O - public List<ResultField> getResultField() {/ }8 ?5 X& B6 |$ c" ?3 C
- return resultField;
, E1 ]) W& [8 s4 }# K% l$ L5 ` - }8 Z2 F$ p, v) r0 k9 N X, |+ O
- public void setResultField(List<ResultField> resultField) {
/ f! v4 `0 E+ `6 u, v4 g& ~ - this.resultField = resultField;
* ]2 Q: F1 Q3 n! c - }
, j! A5 ~+ o- T6 c( U( y - public String getFileName() {/ y5 Z4 q3 }! U1 T. g6 O
- return fileName;
$ [2 S1 U& V; O1 {2 P. l- c - }
& V5 l& j$ j6 [- B% T* Q - public void setFileName(String fileName) {* `1 t' V* |( g4 |3 e8 B
- this.fileName = fileName;1 O; E5 E1 \# e' ^$ Y+ N/ O
- }$ c/ q7 A4 t4 M: Q" @& g
-
3 R, \. @+ E) e& n# j9 d) W - }
复制代码 在转化的时候需要这样来写:& X L: B3 b( b1 t T, t
- Map<String, Class> classMap = new HashMap<String, Class>();
+ D* E! q8 t6 c0 h5 X& W - & N/ m, B7 G& w7 T
- classMap.put("regularCondtionList", Condition.class);; J+ a! t7 F: ]1 `
* h7 B6 b. _# p- classMap.put("dynamicConFieldList", ConditionField.class);/ I0 ]) w3 M5 ?0 `
$ O% }& U3 L- b6 V4 T+ z: A1 Q1 C3 R- classMap.put("resultField", ResultField.class);/ F5 _+ T! m" E) |1 Z8 `( G
- 5 ?/ o2 d' s' C4 B$ {8 q
- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码 + h y3 e& i1 l! b9 O B( I
0 H0 D2 F1 u7 V/ D' e/ i2 M7 c
" J( [# d1 x9 o( N% [: {
# X6 [+ n, Y" O% a8 a, S& K
4 t3 |7 S* l) \
|
|