将 Java 对象转换为 Java Map<String,Object>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12927426/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:49:02  来源:igfitidea点击:

Cast Java Object into Java Map<String,Object>

javajsonmapcastingjetty

提问by KoichiSenada

I am using org.eclipse.jetty.util.ajax.JSONto parse JSON text. But the JSON.parse(string)method produces an Object and I need it as a Map. Internally it is an object of exactly the mentioned class. But how do you cast an Object into a Map without constructing a new one or getting the unchecked cast warning?

我正在使用org.eclipse.jetty.util.ajax.JSON来解析 JSON 文本。但是JSON.parse(string)方法生成一个对象,我需要它作为地图。在内部,它正是上述类的对象。但是,如何在不构建新对象或收到未经检查的强制转换警告的情况下将对象转换为 Map 呢?

Currently, I have found only one solution without the unchecked cast warning, but with constructing a new Map, which is actually of course not a casting at all.

目前,我只找到了一个没有 unchecked cast 警告的解决方案,但是构建了一个新的 Map,这实际上根本不是一个转换。

private Map<String,Object> getMap(String string) {
    HashMap<String,Object> result = new HashMap<>();
    Object object = JSON.parse(string);
    if (object instanceof Map) {
        Map<?,?> map = (Map)(object);
        for (Map.Entry<?,?> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            Object value = entry.getValue();
            result.put(key,value);
        }
    }
    return result;
}

So whether is there a way to properly cast it without unchecked cast warnings?

那么是否有一种方法可以在没有未经检查的投射警告的情况下正确投射它?

回答by Juan Mendes

The compiler can't guarantee that the cast is safe. Since you are the one making the guarantee, you should use @SuppressWarnings("unchecked")

编译器不能保证强制转换是安全的。既然你是做出保证的人,你应该使用@SuppressWarnings("unchecked")

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SuppressWarnings.html

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/SuppressWarnings.html

As @TedHopp points out, the way that library is supposed to be used is that you cast each value in the returnd Objectto the type you know it is (but you would have to cast every property you retrieve) See the mappings here http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/util/ajax/JSON.html

正如@TedHopp 指出的那样,应该使用该库的方式是将返回的每个值转换Object为您知道的类型(但您必须转换您检索的每个属性)请参阅此处的映射http:/ /download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/util/ajax/JSON.html

The point that it brings out, is that you are guaranteeing that this JSON object only contains other JSON objects (map to objects)

它提出的一点是,您保证此 JSON 对象仅包含其他 JSON 对象(映射到对象)

Therefore, if for some reason you're passed the input

因此,如果由于某种原因您通过了输入

// properties are not quoted for readability
{ a: 2, b : {c:3} }

Your code would fail with an invalid cast exception when you try

当您尝试时,您的代码将因无效的强制转换异常而失败

map.get("a")

So remember you're the one guaranteeing what goes into that string you're parsing into JSON

所以请记住,您是保证解析为 JSON 的字符串中的内容的人

If you can't guarantee it, you can't create this getMap function you would like. You have to do the casting (and @SupressWarnings) at the place that knows what type a specific object is.

如果你不能保证它,你就不能创建你想要的这个 getMap 函数。您必须@SupressWarnings在知道特定对象是什么类型的地方进行转换(和)。

For some type safety when working with JSON, you should learn about

对于使用 JSON 时的某些类型安全,您应该了解

Those classes allow you to read JSON directly into Java classes

这些类允许您将 JSON 直接读入 Java 类