java 如何从对象中提取数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12812708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:19:15  来源:igfitidea点击:

How to extract data from object?

javasqlhibernate

提问by user1671219

[56, SensIOP, 9, Open Point] - Index 0
[562, SensIOP, 92, Open Point2] - Index 1

This is my object I am getting from the database call.I am iterating over the list to get this ,now my question here is 1st and 3rd field are long and others are string , how can I extract this from the object , to assign it to some variable.

这是我从数据库调用中获取的对象。我正在遍历列表以获取此对象,现在我的问题是第一个和第三个字段是长字段,其他字段是字符串,我如何从对象中提取它并分配它到一些变量。

Update :

更新 :

 Object object = (Object) iterator.next(); 

This object has the value set one (Index 0 )(It's in for loop)

此对象的值设置为一(索引 0 )(在 for 循环中)

Update 2 :

更新2:

  for (Iterator iterator = List2.iterator(); iterator.hasNext();) {
        Object object = (Object) iterator.next();

回答by subodh

You have to cust it into a long for long property to assign.

您必须将其转换为 long for long 属性才能分配。

for example

例如

Long l = (Long) yourOblList.iterator().next();

see why generic comes generic

看看为什么泛型变得泛型

回答by jainvikram444

using this function you can get result :

 String a1= arrayname[0] ; //[56, SensIOP, 9, Open Point] 

 a1=split(',',a1);
 String Fisrtval=a1[0];
 String Thirdtval=a1[2];

Please refer : http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/util/StringHelper.html#split(java.lang.String, java.lang.String)

请参考:http: //docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/util/StringHelper.html#split(java.lang.String , java.lang.String)

回答by RAS

I don't know how you're fetching data, but when I get data in the format specified by you, I do the following:

我不知道您是如何获取数据的,但是当我以您指定的格式获取数据时,我会执行以下操作:

List<Object[]> objects = query.list();
if (objects.size() > 0) {
    for (int j = 0; j < objects.size(); j++) {
        Object[] object = objects.get(j);
        BigInteger firstValue = (BigInteger) object[0];
        String secondValue = (String) object[1];
        BigInteger thirdValue = (BigInteger) object[2];
        String forthValue = (String) object[3];

        long firstValueLong = firstValue.longValue();
        long thirdValueLong = thirdValue.longValue();
    }
}

回答by Ravinath

I found the result with the help of 'RAS' post its works greate thanks

我在“RAS”的帮助下找到了结果,非常感谢

List<Object> resultList =(List<Object>) em.createNativeQuery("SELECT isbillpayable,isothracctrfable FROM usrfacaccounts ").getResultList();

          for (Iterator<Object> it = resultList.iterator(); it.hasNext();) {
            Object[] object = (Object[]) it.next();
            String firstValue = (String) object[0];
            System.out.println("isbillpayable"+firstValue);
        }