tags:

views:

113

answers:

2

Hello,

I am using a web service that returns a plain object of the type "Object". Debug shows clearly that there is some sort of Array in this object (see the image below) so I was wondering how I can cast this "Object" to an Array (or similar)?

Debug Image

I tried the following:

Collection<String> arr = (Collection<String>) values;
Vector<String> arr = (Vector<String>) values;
ArrayList<String> arr = (ArrayList<String>) values;

But nothing worked. I always get an InvocationTargetException.

What am I doing wrong?

+2  A: 

Your values object is obviously an Object[] comtaining a String[] containing the values.

String[] stringValues = (String[])values[0];
gustafc
That did the trick! Thank you very much!
Jens
+1  A: 

What you've got (according to the debug image) is an object array containing a string array. So you need something like:

Object[] objects = (Object[]) values;
String[] strings = (String[]) objects[0];

You haven't shown the type of values - if this is already Object[] then you could just use (String[])values[0].

Of course even with the cast to Object[] you could still do it in one statement, but it's ugly:

String[] strings = (String[]) ((Object[])values)[0];
Jon Skeet