将Java对象转换为Java Map< String,Object> [英] Cast Java Object into Java Map<String,Object>

查看:1084
本文介绍了将Java对象转换为Java Map< String,Object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 org.eclipse.jetty.util.ajax.JSON 来解析JSON文本。
但是 JSON.parse(string)方法生成一个对象,我需要它作为一个Map。
在内部,它是一个正是提到的类的对象。
但是,如何将一个Object转换成一个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();
对象值= entry.getValue();
result.put(key,value);
}
}
返回结果;因此,是否有一种方法可以正确投射它没有未选中的强制转换警告? / p>

解决方案

编译器不能保证转换是安全的。由于您是作出保证的人,您应该使用 @SuppressWarnings(unchecked)



http://docs.oracle.com/javase/1.5。 0 / docs / api / java / lang / SuppressWarnings.html



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



它带出的观点是, JSON对象只包含其他JSON对象(映射到对象)



因此,如果由于某种原因传递了输入

  //属性没有引用可读性
{a:2,b:{c:3}}
/ pre>

尝试

时,您的代码将失败并出现无效的投放例外。

  map.get(a)

所以记住你是一个保证



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



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





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


I am using org.eclipse.jetty.util.ajax.JSON to 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?

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.

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?

解决方案

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

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 Object to 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

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

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

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.

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

Those classes allow you to read JSON directly into Java classes

这篇关于将Java对象转换为Java Map&lt; String,Object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆