TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名、年龄等# T# T" Q% {- b! C9 U/ W
但是如果存在List集合就会出新net.sf.ezmorph.bean.MorphDynaBean cannot be cast to 错误。
% m( N( E6 T, x& H" _解决方法:- public class Student implements java.io.Serializable{& b! J+ Z! Y1 h8 w
- ; ~3 w- ~7 K# o
- /** ! m5 P8 a3 ?; ?" ~2 B$ `2 f
- * @Fields serialVersionUID : TODO
# I, q W8 P! d8 x - */
0 e, y" }$ ^, d& e" O. | - private static final long serialVersionUID = -1943961352036134112L;$ s; u/ L& {4 B
- private String sname;
4 S5 F6 E g: C+ P+ h; j - private Integer age;& L" F) n4 y3 w3 P) j' ^4 R
- public String getSname() {' d! _5 ~& s( f. ]; Y" o! _9 c& {
- return sname;
0 q; p; U" {$ u! X9 f" o - }& Z- f Y, T. M! N. H% B, y
- public void setSname(String sname) {
) X& T/ p# J) D" X! ]" D - this.sname = sname;
! Z" A, o) M9 T9 n# u9 m8 N; X! N - }0 J( o, D$ Q' [* _# w
- public Integer getAge() {/ J0 q8 W v8 D
- return age;8 V* B* H) O/ V& M
- }
0 A+ K6 x+ |7 \5 i1 m6 z - public void setAge(Integer age) {- b0 W* G$ |" @) e
- this.age = age;7 e8 @# e8 w7 Z: c2 J1 A8 Q- o
- }2 {4 W$ S( g: |) D, p$ U
- / A3 ~; @2 ?% F F8 K
- }
. G+ g4 t! t& e8 c
复制代码 由字符串转java bean时可以使用以下方法:
. `0 C8 Q# e, J$ @ M1 H2 h
! J5 Z: v, b( l- z- s7 f- JSONObject jobj = JSONObject.fromObject(str);2 a/ u2 f, f) r3 D7 g) M
- Student s = (Student) JSONObject.tobean(jobj,Student.class);
复制代码
. U: B% h) @0 u! R b+ g是没有问题的,但是如果属性中含有复杂的类型,当其中属性有类似List , Map ,ArrayList、自定义的类型,如List<Teacher> teachers, 就不可以了。 会报错:MorphDynaBean cannot be cast to con.test……
8 B0 ]7 F7 Q' N. X; ~7 j 在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);$ k! c+ ~- A; {' e% D
9 V6 O+ ?* Z% t; b; m
比如我定义了如下类型:3 L" M. e7 ^5 x, i m! J4 Z! W" ~4 m
- import java.util.List;6 ^+ ?9 S' h3 ~& w- T* b
- /**2 F9 C" `) c" @! p+ t, U
- *
- w7 o3 k* Z- m$ b7 w- g' b - * <P>Tiltle: AnalysisModel.java </P>
7 H6 | V7 N. O, R1 `# d - * <P>Description:模型类 </P>
/ E2 B+ l1 H; f u. z - * @author biyutong1 c8 w4 H" e) B- M+ u1 j# k
- * @date Jun 11, 2012 9:37:54 AM 0 b! r1 C- l( R9 [% D) ]8 C3 z4 H# G
- * @version:V1.0
# ] y, Z. T- Q, @4 o - */
z& u, R/ d9 S! }: G0 j - public class AnalysisModel implements java.io.Serializable{7 ?; E1 y& j# u7 e
8 f( d5 Q Q) n9 p5 W& a- /**) A$ b5 l0 P4 ^1 _8 V G) h' i
- * 固定条件& {& S( A7 L- Q; H% n
- */
: M2 k: z$ G" a! c( k - private List<Condition> regularCondtionList;//6 U2 ^4 n! i6 ~ z
- /**
# q. ~7 N% Y6 K7 y8 a9 A4 x ` - * 动态条件
( M1 ]" R% h! A# W - */
$ t+ q" k2 k& s/ g6 u7 a: ] - private List<ConditionField> dynamicConFieldList;
5 r: y1 J! J$ x7 q& V4 `0 r - /** Z" m! E E. m- s$ E
- * 结果3 \9 |' ], K8 J, u: u
- */; r" E' A. N1 R: H
- private List<ResultField> resultField;
. O: d6 k: v* J& e - /**8 H# _- [" L: o- H3 J$ L
- * 获取前多少条数据0 W% ^+ L0 A" {" ?% ~, D
- */; o' _1 h5 J- p6 O {) j( \6 W% I
- private Integer topnum;
3 ~# v; e+ |* R( B" r - /**7 ^ u' L! ~2 ]4 b% U, |( |2 I
- * 顺序还是随机; Z/ M: E" `1 k% o7 M3 n8 I5 c
- */
) _5 j. |8 [% R$ U' Z$ e/ Z) J/ v - private String sortFlag;1 {' ]. y+ R4 v2 J O5 U
- /**
* n! V) T$ T' Y& B _% | - * 备注5 ?, m% C$ ^9 I3 @
- */; P$ S. z) j, p/ D& P6 h
- private String remark;2 k9 n% c. Y9 x% P+ E5 Y
- /**! U% N/ o% Z% ^+ n: A% V
- * 模型名称- c) @( j/ R4 f0 A! o7 | c
- */
% B, l. s/ g& _ R" u+ C: \. z - private String modelName;+ I; J0 z) E* u2 y1 Y* T# w4 W' W
- /**
, W9 K- Q7 Q! t. m7 U. ?( w' e - * 对应的xml文件名称
; S, [3 c- N+ T/ k# i$ H' J- N! h1 R - */+ w e4 k8 D. E+ ^ ^9 W+ Z! s
- private String fileName;
) W! _8 ]) n4 ?* r" D) H4 @9 m - public List<Condition> getRegularCondtionList() {
2 O- S/ a& [! `# _; X - return regularCondtionList;
0 P D1 b$ }& f: ` - }
( T8 P& p, @8 E1 B( f - public void setRegularCondtionList(List<Condition> regularCondtionList) {1 I N1 }3 P, v$ A# X6 i
- this.regularCondtionList = regularCondtionList;& {8 T3 \5 Z0 f Q( D+ i
- }) y2 _; d2 s& d3 f/ d; i0 X
- public List<ConditionField> getDynamicConFieldList() {
% p% I( X# r; X, K3 A' l3 _ - return dynamicConFieldList;
% G2 q8 N$ c5 D3 g - }) ~* e5 H* z3 r$ K
- public void setDynamicConFieldList(List<ConditionField> dynamicConFieldList) {( `! y& L) n$ a4 d; X4 p
- this.dynamicConFieldList = dynamicConFieldList;
" N( l- S# a7 Q1 o { - }5 }# k, y' e# s8 Z
- public Integer getTopnum() {6 c# A$ v1 k6 f D) l) X
- return topnum;
, o# @$ b9 l3 U - }
! \+ S I9 ?9 z% | - public void setTopnum(Integer topnum) {
& U! t/ y6 R: X - this.topnum = topnum;' P& y5 u5 j3 h9 B
- }
6 m3 w+ ~/ f4 x - public String getSortFlag() {" m1 v' F$ E* S
- return sortFlag;( W; S# @6 A# |* f$ b0 N
- }
+ |. t/ [, W k n8 s) q" P - public void setSortFlag(String sortFlag) {" b, r3 \$ r, {, v! Z# e/ x
- this.sortFlag = sortFlag;
7 H3 K5 p& X8 F( N! k# N/ n/ |3 [8 G - }, w, U+ b3 K9 f! T( q3 y+ b" z
- public String getRemark() {% s! ^6 o J7 [4 _9 B
- return remark;+ G6 I1 b) V8 M6 O* o+ N0 O
- }
2 a" `* v4 m# M - public void setRemark(String remark) {
/ h2 S4 N) L2 d3 f& k4 z* D3 P+ p - this.remark = remark;8 `/ g) @) z j, T
- }
/ ~5 c. ?# c7 t. f8 a1 K! H6 C - ) o1 o5 x; w3 Z- z
-
6 [$ X1 V$ F- ]8 A/ V4 p - public String getModelName() { h! {! n, H3 Y' V
- return modelName;6 j" Y) k4 [0 V% t) s
- }
; z9 W& Y5 {/ K+ `( s$ W# u - public void setModelName(String modelName) {
4 ]0 K; O# w8 ~7 D/ ]' ]/ K - this.modelName = modelName;) d5 p3 z8 e0 F. L! [" \3 o w4 v
- }
( G" x' @) i( u$ R1 \ r - public List<ResultField> getResultField() {' B5 u4 N6 p$ G3 s5 ?
- return resultField;
+ v; c6 Z) X# m' C8 w5 p4 ~/ O1 v - }
. n* Z1 p: B% P& c& } - public void setResultField(List<ResultField> resultField) { L; ^0 h% B3 Z0 w$ l
- this.resultField = resultField;
R$ o- s# B9 O, r - }" c4 \$ P2 Y2 z- h
- public String getFileName() {
$ R8 y: L# j4 }9 |! U/ p - return fileName;0 N- Y; T4 ^1 t. X8 s3 i! L; A7 v
- }, o$ [8 K2 h5 L" G; ]
- public void setFileName(String fileName) {
7 z7 }7 H, g4 D - this.fileName = fileName;* `9 p7 x3 _7 \( p# E
- }$ e! g/ ~6 U% N: C
-
; } b; P/ Q/ U+ _0 J# U - }
复制代码 在转化的时候需要这样来写:# i' f3 C! Q7 n( [! m* W
- Map<String, Class> classMap = new HashMap<String, Class>();
8 G! }9 F" S* G. y7 R
& S2 W$ U: ~3 N" Y6 b( N- classMap.put("regularCondtionList", Condition.class);
4 b m9 q: \2 S( o0 w5 R9 I - 2 {: F+ w5 V3 P
- classMap.put("dynamicConFieldList", ConditionField.class);3 p, {6 I4 d, Z& i
- - |, `5 T7 F) l2 K
- classMap.put("resultField", ResultField.class);+ @$ Z4 C4 k) @( E3 q
- 3 _2 X" E( A! @% P
- AnalysisModel analysisModel=(AnalysisModel) JSONObject.toBean(str, AnalysisModel.class, classMap);
复制代码
* }2 x' S. b* Y' P4 K" P1 U* S8 j- N) C1 H) t1 Z# _% Z! s' u6 j" S
' ?$ N! b0 I' l0 {* T. I
# e! V3 K; I7 {* `& x
! r6 Q9 t( e7 `& n' D |
|