5

I have an object in my code of the type Object: Object o

The class of the instance is Object: o.getClass() gives Object.

Now, it should be a Map! How can I upcast this to a Map?

I tried: Map<String, Object> map = (HashMap<String,Object>)o

But this returns: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.HashMap

The instance 'o' comes from a XMLRPC request. This request does not qualify variables correctly supposedly

Please have a look at this!?

EDIT:

Now I have the following issue: o.getClass().getName() gives java.util.ArrayList,

o.get(0) gives [Ljava.lang.Object;@739e8329,

and o.get(0).getClass().getName() gives java.lang.String.

I cannot findout what to do..

EDIT2:

Finally I found out what happened. The software that created this object flattened a datastructure into a String (valueOf()). So, when I printed the variable it returned a [Ljava.util.Object, which was in fact a String containing this information.

Thanks guys!

user441174
  • 53
  • 1
  • 1
  • 5
  • Possible duplicate of [java: what is this: \[Ljava.lang.Object;?](https://stackoverflow.com/questions/3442090/java-what-is-this-ljava-lang-object) – Dorian Gray Sep 19 '17 at 13:37

4 Answers4

10

[Ljava.lang.Object indicates the type of the object o is an array of Objects - that is Object[]. You cannot cast it to Map.

You might find it useful if took a look at: java: what is this: [Ljava.lang.Object;?

You stated that .getClass() indicated Object, but was it Object or [LObject? Compare to:

    Object[] array= new Object[]{};
    Object simple = new Object();

    System.out.println(array.getClass());
    System.out.println(simple.getClass());      

which prints:

class [Ljava.lang.Object;
class java.lang.Object
Community
  • 1
  • 1
Grzegorz Oledzki
  • 23,614
  • 16
  • 68
  • 106
  • +1 but imho you should add that casting Object to Map is fine, even if you say it indirectly. – InsertNickHere Sep 07 '10 at 07:43
  • I'm now a step further: I do the following: o.getClass().getName() --> java.util.ArrayList, and then o.get(0) --> [Ljava.lang.Object;@8sfajb8c or so, and then o.get(0).getClass().getName() --> java.lang.String (!!) How is that possible? – user441174 Sep 07 '10 at 07:53
  • I think its just that you dont get what you expect to get. – InsertNickHere Sep 07 '10 at 07:59
  • @InsertNickHere, I think you're right.. I have to explore what I'm getting. In PHP I would use var_dump, but it is hard to do that in Java. Any ideas? – user441174 Sep 07 '10 at 08:05
  • @user441174 use any IDE with a debugger (like Eclipse). When breaking you can inspect the variable. You'll see its type, members; pretty much anything you want to know about it. – Joeri Hendrickx Sep 07 '10 at 08:10
  • @Joeri Hendrickx: thanks, I was using a product that is based on Eclipse and it seemed to be dissapeared. Now I switched it on, it is working much better. Thanks. – user441174 Sep 07 '10 at 13:24
8

The error clearly indicates, that o does not implement the Map interface. So it is impossible to cast this object to Map.

The result is an array of Objects. Maybe, the array actually holds maps. Try if this works:

 Object[] objects = (Object[]) o;
 if (objects != null && objects.length > 0) {
    Object object = objects[0];
    if (object instanceof Map) {
      Map map = (Map) object;
      System.out.println("Heureka!");
    }
 }
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I beg to differ on that point, Hashmap does inherit java.lang.Ojbect. Map is an interface, which I understood to be different but not the particular issue here. – ebt Sep 07 '10 at 07:20
  • @ebt - this is exactly the issue. `o` extends `Object`, that's for sure, but to be able to cast this instance to `Map`, the instance has to be an implementation of `Map` too. (Actually he's casting to `HashMap` which is not necessary and doesn't change anything) – Andreas Dolk Sep 07 '10 at 07:23
2
ObjectMapper oMapper = new ObjectMapper();
    Map<String, String> map = oMapper.convertValue(YOUROBJECT, Map.class);
    List<InputValues> inputValuesList = new ArrayList<InputValues>();
   for(String key : map.keySet()){
        inputValuesList.add(new InputValues(key,map.get(key).toString()) );
    }


    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.9</version>
    </dependency>

InputValues class has 2 Strings.Also Jackson Dependency has to be added.

Darshil Shah
  • 447
  • 1
  • 11
  • 21
1

You cannot cast o to Map, because it does not implement Map interface. Exception shows that o is array of Objects.

amorfis
  • 15,390
  • 15
  • 77
  • 125