第一个JAVABEAN:
public class ResultRules {
?private String gTaskId;
?private int ulRet;
?private String execStatus;
?private int ulRetParam1;
?private int ulRetParam2;
?private int ulTaskStatus;
?private String cmdName;
?private int nNUms;
?private List<Rules> rules;
?public String getgTaskId() {
??return gTaskId;
?}
?public void setgTaskId(String gTaskId) {
??this.gTaskId = gTaskId;
?}
?public int getUlRet() {
??return ulRet;
?}
?public void setUlRet(int ulRet) {
??this.ulRet = ulRet;
?}
?public String getExecStatus() {
??return execStatus;
?}
?public void setExecStatus(String execStatus) {
??this.execStatus = execStatus;
?}
?public int getUlRetParam1() {
??return ulRetParam1;
?}
?public void setUlRetParam1(int ulRetParam1) {
??this.ulRetParam1 = ulRetParam1;
?}
?public int getUlRetParam2() {
??return ulRetParam2;
?}
?public void setUlRetParam2(int ulRetParam2) {
??this.ulRetParam2 = ulRetParam2;
?}
?public int getUlTaskStatus() {
??return ulTaskStatus;
?}
?public void setUlTaskStatus(int ulTaskStatus) {
??this.ulTaskStatus = ulTaskStatus;
?}
?public String getCmdName() {
??return cmdName;
?}
?public void setCmdName(String cmdName) {
??this.cmdName = cmdName;
?}
?public int getnNUms() {
??return nNUms;
?}
?public void setnNUms(int nNUms) {
??this.nNUms = nNUms;
?}
?public List<Rules> getRules() {
??return rules;
?}
?public void setRules(List<Rules> rules) {
??this.rules = rules;
?}
}
?
第二个JAVABEAN:
public class Rules {
?private int nSnmpTry;
?private int nSnmpVersion;
?private String szCommunity;
?private int nType;
?private int nCurrentTick;
?private String szParam;
?private String szOid;
?private int PollTime;
?private String szUpdateTime;
?private String szCreateTime;
?private String szSnmpIP;
?private int ID;
?private int nSnmpTimeOut;
?private int nSnmpPort;
?public int getnSnmpTry() {
??return nSnmpTry;
?}
?public void setnSnmpTry(int nSnmpTry) {
??this.nSnmpTry = nSnmpTry;
?}
?public int getnSnmpVersion() {
??return nSnmpVersion;
?}
?public void setnSnmpVersion(int nSnmpVersion) {
??this.nSnmpVersion = nSnmpVersion;
?}
?public String getSzCommunity() {
??return szCommunity;
?}
?public void setSzCommunity(String szCommunity) {
??this.szCommunity = szCommunity;
?}
?public int getnType() {
??return nType;
?}
?public void setnType(int nType) {
??this.nType = nType;
?}
?public int getnCurrentTick() {
??return nCurrentTick;
?}
?public void setnCurrentTick(int nCurrentTick) {
??this.nCurrentTick = nCurrentTick;
?}
?public String getSzParam() {
??return szParam;
?}
?public void setSzParam(String szParam) {
??this.szParam = szParam;
?}
?public String getSzOid() {
??return szOid;
?}
?public void setSzOid(String szOid) {
??this.szOid = szOid;
?}
?public int getPollTime() {
??return PollTime;
?}
?public void setPollTime(int pollTime) {
??PollTime = pollTime;
?}
?public String getSzUpdateTime() {
??return szUpdateTime;
?}
?public void setSzUpdateTime(String szUpdateTime) {
??this.szUpdateTime = szUpdateTime;
?}
?public String getSzCreateTime() {
??return szCreateTime;
?}
?public void setSzCreateTime(String szCreateTime) {
??this.szCreateTime = szCreateTime;
?}
?public String getSzSnmpIP() {
??return szSnmpIP;
?}
?public void setSzSnmpIP(String szSnmpIP) {
??this.szSnmpIP = szSnmpIP;
?}
?public int getID() {
??return ID;
?}
?public void setID(int iD) {
??ID = iD;
?}
?public int getnSnmpTimeOut() {
??return nSnmpTimeOut;
?}
?public void setnSnmpTimeOut(int nSnmpTimeOut) {
??this.nSnmpTimeOut = nSnmpTimeOut;
?}
?public int getnSnmpPort() {
??return nSnmpPort;
?}
?public void setnSnmpPort(int nSnmpPort) {
??this.nSnmpPort = nSnmpPort;
?}
?public Rules() {
?}
?public Rules(int nSnmpTry, int nSnmpVersion, String szCommunity, int nType, int nCurrentTick, String szParam, String szOid, int pollTime, String szUpdateTime, String szCreateTime, String szSnmpIP, int iD, int nSnmpTimeOut, int nSnmpPort) {
??super();
??this.nSnmpTry = nSnmpTry;
??this.nSnmpVersion = nSnmpVersion;
??this.szCommunity = szCommunity;
??this.nType = nType;
??this.nCurrentTick = nCurrentTick;
??this.szParam = szParam;
??this.szOid = szOid;
??this.PollTime = pollTime;
??this.szUpdateTime = szUpdateTime;
??this.szCreateTime = szCreateTime;
??this.szSnmpIP = szSnmpIP;
??this.ID = iD;
??this.nSnmpTimeOut = nSnmpTimeOut;
??this.nSnmpPort = nSnmpPort;
?}
?
调用代码:
JSONArray jsonArray = JSONArray.fromObject(result);
??Map<String, Class> map = new HashMap<String, Class>();
??map.put("rules", Rules.class);
??for (int i = 0; i < jsonArray.size(); i++) {
???JSONObject jsonObject = (JSONObject) jsonArray.get(i);
???ResultRules resultRules = (ResultRules) jsonObject.toBean(jsonObject, ResultRules.class, map);
???System.out.println(resultRules.getUlTaskStatus());
??}
?
?
?
报错情况如下:
net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'rules' on class 'class com.smonitor.bean.ResultRules'
?at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:69)
?at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:60)
?at net.sf.json.JSONObject.setProperty(JSONObject.java:1394)
?at net.sf.json.JSONObject.toBean(JSONObject.java:422)
?at net.sf.json.JSONObject.toBean(JSONObject.java:252)
?at com.smonitor.web.CommandAction.doPost(CommandAction.java:74)
?at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
?at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
?at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
?at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
?at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
?at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
?at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
?at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
?at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
?at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
?at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
?at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
?at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
?at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoSuchMethodException: Unknown property 'rules' on class 'class com.smonitor.bean.ResultRules'
?at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2133)
?at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:890)
?at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:67)
?... 19 more
?