Java 找出HashMap中使用的类型

Java 找出HashMap中使用的类型,java,json,hashmap,instanceof,Java,Json,Hashmap,Instanceof,我有一个从JSON文件填充的HashMap。键值对中的值可以是两种不同的类型-字符串或另一个键值对 例如: HashMap HashMap=newmap() JSON文件看起来有点像这样: "custom": { "mappedReference": {"source": "someMappedReference"}, "somethingElse": "some literal" } } 稍后在填充hashMap之后,当我遍历时,我需要检

我有一个从JSON文件填充的HashMap。键值对中的值可以是两种不同的类型-字符串或另一个键值对

例如:

HashMap HashMap=newmap()

JSON文件看起来有点像这样:

    "custom": {
       "mappedReference": {"source": "someMappedReference"},
       "somethingElse": "some literal"
     }
    }
稍后在填充hashMap之后,当我遍历时,我需要检查该值是hashMap类型还是String类型。我尝试了很多方法,但似乎无法获得地图中对象的类型

    for(Map.Entry<String,Object> m : hashMap.entrySet())
    {
        final String key = m.getKey();
        final Object value = m.getValue();

        if(value instanceof Map<String,String>) 
        {
            //get key and value of the inner key-value pair...
        }
        else 
        {
            //it's just a string and do what I need with a String.
        }

    }
for(Map.Entry m:hashMap.entrySet())
{
最后一个字符串key=m.getKey();
最终对象值=m.getValue();
if(映射的值实例)
{
//获取内部键值对的键和值。。。
}
其他的
{
//这只是一根绳子,用它做我需要的事。
}
}
关于如何从地图中获取数据类型,有什么想法吗?提前感谢

您可以像下面这样使用

ParameterizedType pt = (ParameterizedType)Generic.class.getDeclaredField("map").getGenericType();
            for(Type type : pt.getActualTypeArguments()) {
                System.out.println(type.toString());
Ref:

您可以像下面这样使用

ParameterizedType pt = (ParameterizedType)Generic.class.getDeclaredField("map").getGenericType();
            for(Type type : pt.getActualTypeArguments()) {
                System.out.println(type.toString());

参考:我发布了一个类似问题的答案:

这是:

通常不赞成不必要地使用对象类型。但是,根据您的情况,您可能必须有一个HashMap,尽管它是最好避免的。也就是说,如果您必须使用一个,这里有一个简短的代码片段可能会有所帮助。它使用instanceof

Map<String, Object> map = new HashMap<String, Object>();

for (Map.Entry<String, Object> e : map.entrySet()) {
    if (e.getValue() instanceof Integer) {
        // Do Integer things
    } else if (e.getValue() instanceof String) {
        // Do String things
    } else if (e.getValue() instanceof Long) {
        // Do Long things
    } else {
        // Do other thing, probably want error or print statement
    }
}
Map Map=newhashmap();
对于(Map.Entry e:Map.entrySet()){
if(例如,getValue()instanceof Integer){
//做整型的事情
}else if(例如,字符串的getValue()实例){
//做串东西
}else if(例如getValue()instanceof Long){
//做长事情
}否则{
//做其他事情,可能需要错误或打印语句
}
}

我发布了一个类似问题的答案:

这是:

通常不赞成不必要地使用对象类型。但是,根据您的情况,您可能必须有一个HashMap,尽管它是最好避免的。也就是说,如果您必须使用一个,这里有一个简短的代码片段可能会有所帮助。它使用instanceof

Map<String, Object> map = new HashMap<String, Object>();

for (Map.Entry<String, Object> e : map.entrySet()) {
    if (e.getValue() instanceof Integer) {
        // Do Integer things
    } else if (e.getValue() instanceof String) {
        // Do String things
    } else if (e.getValue() instanceof Long) {
        // Do Long things
    } else {
        // Do other thing, probably want error or print statement
    }
}
Map Map=newhashmap();
对于(Map.Entry e:Map.entrySet()){
if(例如,getValue()instanceof Integer){
//做整型的事情
}else if(例如,字符串的getValue()实例){
//做串东西
}else if(例如getValue()instanceof Long){
//做长事情
}否则{
//做其他事情,可能需要错误或打印语句
}
}

map.get(key.getClass()
key.getClass();地图没有元数据。您可以知道键的类和特定项的值,但不能知道泛型参数。下面是一个类似问题的答案,可能会有所帮助:下面是一个类似问题的答案,可能会有所帮助:
map.get(key).getClass()
key.getClass();地图没有元数据。您可以知道键的类别和特定项的值,但不是通用参数。这是一个类似问题的答案,可能会有帮助:这是一个类似问题的答案,可能会有帮助:在这种情况下不可能:我们感兴趣的地图是从地图的值中获得的。在这种情况下不可能:我们感兴趣的地图是从地图的值中获得的。我只是休息几天后,我又回去工作了。我打算为那些发表意见的人发布一个答案。这正是我在上面的问题中得出的结论。我会记下这一点作为答案。休息几天后我刚回来工作。我打算为那些发表意见的人发布一个答案。这正是我在上面的问题中得出的结论。我将把这个标记为答案。