简体   繁体   English

以对象为值在HashMap中动态嵌套键值

[英]Dynamically nest key value in HashMap with object as value

I have a requirement to nest a key-value in an existing key value pair of a HashMap.我需要在 HashMap 的现有键值对中嵌套一个键值。 my current output as json is我当前的 json 输出是

...
    "x": {
      "y": yhyhy,
      "z": "zhzhz",
      "d": 123
    }
...

I want to add a new pair as follows:我想添加一个新对如下:

...
    "x": {
      "y": yhyhy,
      "z": "zhzhz",
      "d": 123,
      "k": "khkhkh"
    }
..

The issue with achieving this is : the hashmap prepared initially is by inserting an entire object as value (reposneObject is an immutableMap being used elsewhere)实现这一点的问题是:最初准备的哈希图是通过插入整个对象作为值(reposneObject 是一个在别处使用的 immutableMap)

Map<Object, Object> responseMap = new HashMap<>(responseObject);
                    responseMap.put("x", someObject);

I do not want to separately insert each field of the someObject into the map (as the count can be huge) Is there any other way of doing it using maps or something?我不想将 someObject 的每个字段单独插入到地图中(因为计数可能很大)有没有其他方法可以使用地图或其他东西来做到这一点? (dynamically adding a field in an object doesn't seem possible) (在对象中动态添加字段似乎不可能)

Assuming someObject is a Map:假设someObject是一个地图:

Map<Object, Object> responseMap = new HashMap<>(responseObject);
responseMap.put("x", someObject);
responseMap.get("x").put("k": "khkhkh");  

Is there any other way of doing it using maps or something?有没有其他方法可以使用地图或其他东西来做到这一点?

responseMap.putAll(someObject); where someObject is a map .其中 someObject 是地图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM