简体   繁体   English

如何将对象转发到java.util.Map?

[英]How to upcast an Object to a java.util.Map?

I have an object in my code of the type Object: Object o 我在Object: Object o类型的代码中有一个对象

The class of the instance is Object: o.getClass() gives Object. 实例的类是Object: o.getClass()给出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 我试过: Map<String, Object> map = (HashMap<String,Object>)o

But this returns: java.lang.ClassCastException: [Ljava.lang.Object; 但是这会返回:java.lang.ClassCastException:[Ljava.lang.Object; cannot be cast to java.util.HashMap 无法强制转换为java.util.HashMap

The instance 'o' comes from a XMLRPC request. 实例“o”来自XMLRPC请求。 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.getClass().getName()给出java.util.ArrayList

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

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

I cannot findout what to do.. 我不知道该怎么做..

EDIT2: EDIT2:

Finally I found out what happened. 最后我发现了发生了什么。 The software that created this object flattened a datastructure into a String (valueOf()). 创建此对象的软件将数据结构展平为String(valueOf())。 So, when I printed the variable it returned a [Ljava.util.Object, which was in fact a String containing this information. 所以,当我打印变量时,它返回了一个[Ljava.util.Object,它实际上是一个包含这些信息的String。

Thanks guys! 多谢你们!

[Ljava.lang.Object indicates the type of the object o is an array of Objects - that is Object[] . [Ljava.lang.Object表示对象的类型o是一个对象数组 - 即Object[] You cannot cast it to Map . 你不能把它投射到Map

You might find it useful if took a look at: java: what is this: [Ljava.lang.Object;? 你可能会发现它很有用,如果看一下: java:这是什么:[Ljava.lang.Object;?

You stated that .getClass() indicated Object , but was it Object or [LObject ? 你声明.getClass()指出了Object ,但它是Object还是[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

The error clearly indicates, that o does not implement the Map interface. 该错误清楚地表明, o没有实现Map接口。 So it is impossible to cast this object to Map . 因此无法将此对象转换为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!");
    }
 }

You cannot cast o to Map , because it does not implement Map interface. 你不能将oMap ,因为它没有实现Map接口。 Exception shows that o is array of Object s. 异常显示oObject的数组。

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. InputValues类有2个字符串。还必须添加Jackson Dependency。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM