Java 如何迭代hocon配置对象?
我有以下几点:Java 如何迭代hocon配置对象?,java,playframework,typesafe-config,hocon,Java,Playframework,Typesafe Config,Hocon,我有以下几点: { "child1-name" : { "child1child1-name" : "child1child1-value", "child1child2-name" : "child1child2-value" }, "child2" : { "child2child1-name" : "child2child1-value" }, "child3-name" : "child3-valu
{
"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.
while(iterator.hasNext()){
条目fld=iterator.next();
//现在,在这里访问每个对象和值,它们将是Configvalue类型
//If(ConfigValueType.OBJECT)
//设置所需的值
//else If(ConfigValueType.STRING)
//设置所需的值
}
//迭代完成后,在config中设置新值并返回最终的config对象。
下面是我想到的示例代码
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** .
}
String jsonString=_mapper.writeValueAsString(jsonRoot);//jsonRoot是有效的jsonNode对象
Config=ConfigFactory.parseString(jsonString);
//现在,我想通过迭代config对象来设置注释。
//我已经通过了以下API,
ConfigObject co=config.root();
Set configNode2=co.entrySet();
迭代器itr=configNode2.Iterator();
while(itr.hasNext()){
条目fld=itr.next();
//**如何设置注释并返回配置对象**。
}
将JSON转换为HOCON的主要原因是我需要设置注释。现在在上面的代码中,我不知道如何设置注释。在阅读并搜索了对我来说很复杂的typesafe api之后,我能够通过这样做解决这个问题
List<String> comments = Arrays.asList("A new","comment");
String jsonString = "{ \"a\" : 42 , \"b\" : 18 }";
Config config = ConfigFactory.parseString(jsonString);
ConfigObject co = config.root();
ConfigObject co2 = co;
Set<Entry<String, ConfigValue>> configNode2 = co.entrySet();
Iterator<Entry<String, ConfigValue>> itr = configNode2.iterator();
while(itr.hasNext()){
Entry<String, ConfigValue> fld = itr.next();
String key = fld.getKey();
ConfigValue value = fld.getValue();
ConfigOrigin oldOrigin = value.origin();
ConfigOrigin newOrigin = oldOrigin.withComments(comments);
ConfigValue newValue = value.withOrigin(newOrigin);
// fld.setValue(newValue); // This doesn't work: it's immutable
co2 = co2.withValue(key,newValue);
}
config = co2.toConfig();
System.out.println(config.root().render(ConfigRenderOptions.concise().
setComments(true).setFormatted(true)));
List comments=Arrays.asList(“一个新的”、“注释”);
字符串jsonString=“{\'a\':42,\'b\':18}”;
Config=ConfigFactory.parseString(jsonString);
ConfigObject co=config.root();
二氧化碳=一氧化碳;
Set configNode2=co.entrySet();
迭代器itr=configNode2.Iterator();
while(itr.hasNext()){
条目fld=itr.next();
String key=fld.getKey();
ConfigValue=fld.getValue();
ConfigOrigin oldOrigin=value.origin();
ConfigOrigin newOrigin=oldOrigin.withComments(comments);
ConfigValue newValue=value.withOrigin(newOrigin);
//setValue(newValue);//这不起作用:它是不可变的
co2=co2.withValue(键,新值);
}
config=co2.toConfig();
System.out.println(config.root()。
setComments(true).setFormatted(true));
我希望这对将来的人有帮助 您发布的示例不是JSON。事实上,它看起来更像一个HOCON文件。这里有什么问题?代码的输出是什么?您期望的是什么?现在这个示例被编辑为一个有效的JSON对象,困惑转移到为什么您要尝试使用HOCON基础设施解析JSON对象。现在还不清楚你想在这里做什么。我已经更具体地更新了这个问题。