-1

这是我尝试使用 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 类的样子

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);
4

1 回答 1

0

从你的例外来看,这应该可以工作(我猜......):

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不能转换为org.juzzy.server.ProcessInputs

因为API

将给定的 json 格式输入反序列化为 Java 对象。

但是,如果第一个选项不起作用,则可能存在这种可能性。

于 2016-05-31T13:23:33.003 回答