FlexJson错误:ClassCastException:java.util.HashMap无法强制转换为类

时间:2016-05-31 13:18:07

标签: java json hashmap classcastexception flexjson

这是我尝试使用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);

1 个答案:

答案 0 :(得分: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对象。

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