英文:
Java: get Map from Object
问题
使用以下代码:
Map<String, Object> payloadMap = new HashMap<String, Object>();
payloadMap = (Map<String, Object>) new Gson().fromJson(result, payloadMap.getClass());
,我将这个 JSON 转换为一个具有**无限**数量的子映射的映射:
{
"name":"name1",
"job":"prosecutor",
"department": {
"department_name":"prosecutor's office"
}
}
要获取子(嵌套)映射的值,我尝试从父映射的对象值中获取“包装”的子映射。
```java
public void mapRequestNode(Map<String, Object> payloadMap) {
payloadMap.entrySet().forEach(node -> this.getDataFromNode(node));
}
如上图所示,无法使用被“包装”为对象的子映射“department”。我可以访问对象的方法,但无法访问映射的方法(例如,无法使用“value.get("department_name")”)。我尝试过转换为“(Map<String, Object>) value”,但没有成功...
上面的示例中,“department”名称只是举例说明!我不知道 JSON 子对象的具体名称。可能有无限数量的名称!因此我无法像这样使用“payloadMap.get("department")”
图片链接见原文。
<details>
<summary>英文:</summary>
Using this code
Map<String,Object> payloadMap = new HashMap<String,Object>();
payloadMap = (Map<String,Object>) new Gson().fromJson(result, payloadMap.getClass());
, I convert this json:
{
"name":"name1",
"job":"prosecutor",
"department": {
"department_name":"prosecutor's office"
}
}
to the map (map with **unlimited** number of child maps):
[![enter image description here][1]][1]
This done well, but now I want to get an access to values of child (nested) maps.
In parent map child maps "wrapped" to Object.
So, I tried to get "wrapped" child maps from the Object-values of parent map.
public void mapRequestNode (Map<String,Object> payloadMap) {
payloadMap.entrySet().forEach(node->this.getDataFromNode(node));
}
[![enter image description here][2]][2]
As you can see from the above picture, there are no way to use child map "department", which had been "wrapped" to Object. I have an access to Object-methods, but not to the Map-methods (for example, I cant use "value.get("department_name")". I tried cast "(Map<String, Object>)value", but without success...
The "department" name in case above is only for example! I dont know concrete name of json child-objects. There may be **unlimited** number of names! So I cant use something like this "payloadMap.get("department")"
[1]: https://i.stack.imgur.com/5V7co.png
[2]: https://i.stack.imgur.com/b3rDQ.png
</details>
# 答案1
**得分**: 1
以下是翻译好的内容:
接下来
((Map<String, Object>) payloadMap.get("department")).get("department_name")
应该可以工作,对吧?
<details>
<summary>英文:</summary>
Following
((Map<String, Object>)payloadMap.get("department")).get("department_name")
should work, dont?
</details>
# 答案2
**得分**: 1
你的变量 `value` 的类型是 Object,这意味着编译器不会了解关于这个变量的其他信息。即使你从 JSON 文件中检索到的对象是一个映射(map),由于你将其存储在一个 Object 变量中,编译器会将其处理为 Object 而不是 Map。这就是为什么你不能执行 `value.get("department");`:对于 Object 类型,不存在 `get` 方法。
你需要将存储在 `value.get("department")` 中的内容强制转换为 `Map<String, Object>`,以便能够将其处理为一个映射(Map)。
<details>
<summary>英文:</summary>
Your variable `value` is of type Object, which means that the compiler will not know anything else about the variable. Even if the object you retrieve from your json file is a map, as you store it in a Object variable, the compiler will handle it as an Object and not as a Map. That is why you cannot do `value.get("department");` : the method `get` does not exist for the type Object.
You need to cast whatever is stored in `value.get("department")` as a `Map<String, Object>` to be able to handle it as a Map.
</details>
# 答案3
**得分**: 0
我已经找到了一个特殊的解决方案。
我将json转换为Map<String,Map<String,Object>>。
不是Map<String,Object>。在这种情况下,我可以成功地使用父DTO映射的子映射。
但这个解决方案在意义上是特殊的(不是通用的),因为我可以以这种方式处理仅由对象组成的json。
例如,如果我尝试在以下示例中获取"job"的值:
{
"job": "prosecutor",
"department": {
"department_name":"prosecutor's office"
}
}
通过使用Map.Entry<String, Map<String, Object>> payloadNodeEntry.getValue,
我将收到ClassCastException(无法将String转换为Map<String, Object>)。
<details>
<summary>英文:</summary>
I have found a special solution.
I convert json to Map<String,Map<String,Object>>.
Not in Map<String,Object>. In this case I can successfully use child-maps of parent dto-map.
But this solution is special (not general) in the meaning that, I can handle in this way json, which consist only of objects.
For example, if I try to get the value of "job" in following example:
{
"job": "prosecutor",
"department": {
"department_name":"prosecutor's office"
}
}
by using Map.Entry<String, Map<String, Object>> payloadNodeEntry.getValue,
I will receive ClassCastException (cant cast String to Map<String, Object> ).
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论