如何将对象向上转换为 java.util.Map?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3656485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upcast an Object to a java.util.Map?
提问by user441174
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; cannot be cast to java.util.HashMap
但这会返回: java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 java.util.HashMap
The instance 'o' comes from a XMLRPC request. This request does not qualify variables correctly supposedly
实例“o”来自 XMLRPC 请求。这个请求没有正确地限定变量
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:
编辑2:
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.
终于我知道发生了什么。创建此对象的软件将数据结构扁平化为字符串 (valueOf())。因此,当我打印该变量时,它返回一个 [Ljava.util.Object,它实际上是一个包含此信息的字符串。
Thanks guys!
谢谢你们!
采纳答案by Grzegorz Oledzki
[Ljava.lang.Object
indicates the type of the object o is an array of Objects - that is Object[]
. You cannot cast it to Map
.
[Ljava.lang.Object
表示对象 o 的类型是一个对象数组 - 即Object[]
. 您不能将其转换为Map
.
You might find it useful if took a look at: java: what is this: [Ljava.lang.Object;?
如果查看以下内容,您可能会发现它很有用:java: what is this: [Ljava.lang.Object;?
You stated that .getClass()
indicated Object
, but was it Object
or [LObject
?
Compare to:
你说这.getClass()
表明Object
,但它Object
还是[LObject
?相比于:
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
回答by Andreas Dolk
The error clearly indicates, that o
does not implement the Map
interface. So it is impossible to cast this object to Map
.
错误清楚地表明,o
没有实现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!");
}
}
回答by amorfis
You cannot cast o
to Map
, because it does not implement Map
interface. Exception shows that o
is array of Object
s.
您不能强制转换o
为Map
,因为它没有实现Map
接口。异常表明它o
是Object
s 的数组。
回答by Darshil Shah
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.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.9.9</version>
</dependency>
InputValues class has 2 Strings.Also Hymanson Dependency has to be added.
InputValues 类有 2 个字符串。还必须添加 Hymanson 依赖项。