FlexJson Error : ClassCastException: java.util.HashMap cannot be cast to class

FlexJson Error : ClassCastException: java.util.HashMap cannot be cast to class

这是我尝试使用 FlexJSON 将我的 LONGTEXT 对象从 mysql 转换回我的 ProcessInputs java 对象时遇到的错误。

java.lang.ClassCastException: java.util.HashMap cannot be cast to org.juzzy.server.ProcessInputs
    at org.juzzy.server.FLSDAO.RetrieveFLSFromDatabase(FLSDAO.java:68)
    at org.juzzy.client.CreateFLS.doPost(CreateFLS.java:48)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

这就是我的 ProcessInput class 的样子

public class ProcessInputs {
//inputs contain names of all the inputs and their objects
    Map<String,Input> inputs= new LinkedHashMap<String,Input>();
    Map<String,Output> outputs= new HashMap<String,Output>();
    Map<String,Object> fuzzyTerms=new HashMap<String,Object>();
    Map<String,String> fuzzyTermMapper=new HashMap<String,String>();
    ArrayList<T1_Antecedent> antecedentArrayList;
    String outputxml;
    T1_Rulebase  rulebase = new T1_Rulebase(1);
//getters,setters, constructor and functions
}

我在 FLEXJSON 中的检索代码:

while(rs.next()){
        String jsonInString= rs.getString("fls");
        JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
        ProcessInputs fls = der.deserialize(jsonInString);

    }

序列化是这样发生的:

public boolean InsertFLStoDatabase(ProcessInputs object, String xml) throws IOException {
    Connection connection = DatabaseConnect.getConnection();
    String query = "insert into fls_systems (fls, xml) values (?,?)";
    try { 
        JSONSerializer ser = new JSONSerializer();
        String jsonInString = ser.deepSerialize(object);
        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, jsonInString);

根据你的例外情况,这应该有效(我猜...):

while(rs.next()){
    String jsonInString= rs.getString("fls");
    JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
    // exception is here, right? der.deserialize(String) is giving you a Map!!
    Map<String,Input> fls = der.deserialize(jsonInString);
}

对flexjson了解不深,但我不会像

那样投射
Map<String,Input> fls = (Map<String,Input>) der.deserialize(jsonInString);

因为消息:

java.util.HashMap cannot be cast to org.juzzy.server.ProcessInputs

并且因为 API:

Deserialize the given json formatted input into a Java object.

但如果第一个选项不起作用,则有可能。