我有以下 JSON,
{
"child1-name" : {
"child1child1-name" : "child1child1-value",
"child1child2-name" : "child1child2-value"
},
"child2" : {
"child2child1-name" : "child2child1-value"
},
"child3-name" : "child3-value"
}
现在在这里,因为它是一个 HOCON 配置对象,我想对其进行迭代并递归检索每个元素。我想遍历每个配置对象并根据其类型(ArrayNode、ObjectNode、String 等)设置适当的值(注释)并通过设置最终配置对象返回该节点。
我想实现以下 Pusedo 代码:
while(iterator.hasNext()) {
Entry<String, ConfigValue> fld = iterator.next();
// Now here access each object and value which will be of type of Configvalue
//If(ConfigValueType.OBJECT)
//set the required value
//else If(ConfigValueType.STRING)
//set the required value
}
//Once iteration done, set the new values in config and return final config object.
按照我正在考虑的示例代码,
String jsonString = _mapper.writeValueAsString(jsonRoot); // jsonRoot is valid jsonNode object
Config config = ConfigFactory.parseString(jsonString);
//Now, I want to set comments by iterating the config object.
//I have gone through the following API’s,
ConfigObject co = config.root();
Set<Entry<String, ConfigValue>> configNode2 =co.entrySet();
Iterator<Entry<String, ConfigValue>> itr = configNode2.iterator();
while(itr.hasNext()){
Entry<String, ConfigValue> fld = itr.next();
// **how to set comments and return the config object** .
}
将 JSON 转换为 HOCON 以便我设置评论的主要原因。现在在上面的代码中,我不确定如何设置注释。