user1671219
user1671219

Reputation: 413

How to extract data from object?

[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)

Update 2 :

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

Upvotes: 2

Views: 32895

Answers (4)

Ravinath
Ravinath

Reputation: 1750

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

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

Upvotes: 2

RAS
RAS

Reputation: 8158

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();
    }
}

Upvotes: 2

Vikram Jain
Vikram Jain

Reputation: 5588

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)

Upvotes: 1

subodh
subodh

Reputation: 6158

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

for example

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

see why generic comes generic

Upvotes: 1

Related Questions