4

How to modify an object to a java.util.Map?

 2 years ago
source link: https://www.codesd.com/item/how-to-modify-an-object-to-a-java-util-map.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to modify an object to a java.util.Map?

advertisements

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!


[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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK