【问题标题】:I could not cast integer values to string我无法将整数值转换为字符串
【发布时间】:2013-09-07 10:05:12
【问题描述】:
 Query query = s.createQuery("from ClientList");

 List <String> results = query.list();
 Iterator<String> it = results.iterator();
 while(it.hasNext())
 {
     Object[] row = (Object[]) it.next();

     System.out.println("ReturnValues==========" + results);

     Map<String, String> jsonObject = new HashMap<String, String>();
     jsonObject.put("Record_Id", (String) row[0]);
     jsonObject.put("Client_Code", (String)row[1]);
     jsonObject.put("Client_Description", (String)row[2]);

     returnValues.add(jsonObject);
}

我的表的第一列包含一个整数值。我收到此错误消息:

Exception===java.lang.ClassCastException:  cannot be cast to [Ljava.lang.Object;

【问题讨论】:

  • 这会有所帮助,如果你发布一行的样子。特别是,行中的数据是如何分离的?按空格、分号?

标签: java json hibernate classcastexception


【解决方案1】:

您的迭代器返回一个字符串。您不能将其转换为对象数组。

字符串中有一个split 方法,它通过给定的正则表达式拆分您的字符串并返回一个包含拆分部分的String[]


由于您没有提供更多相关信息,我将假设您的行中的数据由空格分隔。

String row = ll.next() // I assume row = "1234 5678 Description_No_Spaces"
String[] data = row.split("\\s+");

String record_Id = data[0];

【讨论】:

    【解决方案2】:

    您不需要迭代器,您可以使用 foreach 循环遍历 results

    List <String> results =query.list();
    for(String result: results) {
       String[] row = /* user result.split(...)  to get attributes*/
    
     System.out.println("ReturnValues=========="+results);
     Map<String, String> JSonObject=new HashMap<String, String>();
     JSonObject.put("Record_Id", row[0]);
     JSonObject.put("Client_Code", row[1]);
     JSonObject.put("Client_Description",row[2]);
     ReturnValues.add(JSonObject);
    

    }

    查看String.split(String regex) 文档。

    【讨论】:

      【解决方案3】:
      Query query = s.createQuery("from ClientList");
      
       List <String> results = query.list();
       Iterator<String> it = results.iterator();
           while(it.hasNext())`enter code here`
       {
           Object[] row = (Object[]) it.next();
      
           System.out.println("ReturnValues==========" + results);
      
           Map<String, String> jsonObject = new HashMap<String, String>();
           jsonObject.put("Record_Id", String.valueOf(row[0]));
           jsonObject.put("Client_Code", row[1]);
           jsonObject.put("Client_Description", row[2]);
      
           returnValues.add(jsonObject);
      }
      

      希望这能解决你的问题

      【讨论】:

        猜你喜欢
        • 2017-01-25
        • 2012-10-22
        • 2018-01-23
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多