如何在java中将JSON转换为属性文件?
我有JSON字符串,想转换成java属性文件。 注意:JSON可以是JSON字符串、对象或文件。 示例JSON:如何在java中将JSON转换为属性文件?,java,json,properties-file,Java,Json,Properties File,我有JSON字符串,想转换成java属性文件。 注意:JSON可以是JSON字符串、对象或文件。 示例JSON: { "simianarmy": { "chaos": { "enabled": "true", "leashed": "false", "ASG": { "enabled": "false", "probability
{
"simianarmy": {
"chaos": {
"enabled": "true",
"leashed": "false",
"ASG": {
"enabled": "false",
"probability": "6.0",
"maxTerminationsPerDay": "10.0",
"IS": {
"enabled": "true",
"probability": "6",
"maxTerminationsPerDay": "100.0"
},
},
},
}
**OUTPUT SHOULD BE:-**
simianarmy.chaos.enabled=true
simianarmy.chaos.leashed=false
simianarmy.chaos.ASG.enabled=false
simianarmy.chaos.ASG.probability=6.0
simianarmy.chaos.ASG.maxTerminationsPerDay=10.0
simianarmy.chaos.ASG.IS.enabled=true
simianarmy.chaos.ASG.IS.probability=6
simianarmy.chaos.ASG.IS.maxTerminationsPerDay=100.0
您可以使用jackson库中的JavaPropsMapper。但为了能够解析json字符串并从中构造java对象,必须先定义接收json对象的对象层次结构,然后才能使用它
一旦成功地从json构建了java对象,就可以将其转换为Properties对象,然后将其序列化为文件,这将创建所需的内容
json示例:
{ "title" : "Home Page",
"site" : {
"host" : "localhost"
"port" : 8080 ,
"connection" : {
"type" : "TCP",
"timeout" : 30
}
}
}
以及映射上述JSON结构的类层次结构:
class Endpoint {
public String title;
public Site site;
}
class Site {
public String host;
public int port;
public Connection connection;
}
class Connection{
public String type;
public int timeout;
}
因此,您可以从中构造java对象端点并将其转换为属性对象,然后可以序列化为.Properties文件:
打开json.properties文件后,可以看到输出:
site.connection.type=TCP
site.connection.timeout=30
site.port=8080
site.host=localhost
标题=主页
这个想法来自一篇文章
希望这会有所帮助。您可以进行树遍历并以点表示法获取属性 下面是一个树遍历示例
//------------ Transform jackson JsonNode to Map -----------
public static Map<String, String> transformJsonToMap(JsonNode node, String prefix){
Map<String,String> jsonMap = new HashMap<>();
if(node.isArray()) {
//Iterate over all array nodes
int i = 0;
for (JsonNode arrayElement : node) {
jsonMap.putAll(transformJsonToMap(arrayElement, prefix+"[" + i + "]"));
i++;
}
}else if(node.isObject()){
Iterator<String> fieldNames = node.fieldNames();
String curPrefixWithDot = (prefix==null || prefix.trim().length()==0) ? "" : prefix+".";
//list all keys and values
while(fieldNames.hasNext()){
String fieldName = fieldNames.next();
JsonNode fieldValue = node.get(fieldName);
jsonMap.putAll(transformJsonToMap(fieldValue, curPrefixWithDot+fieldName));
}
}else {
//basic type
jsonMap.put(prefix,node.asText());
System.out.println(prefix+"="+node.asText());
}
return jsonMap;
}
这里是一个使用队列而不是递归的迭代版本
//------------ Transform jackson JsonNode to Map -Iterative version -----------
public static Map<String,String> transformJsonToMapIterative(JsonNode node){
Map<String,String> jsonMap = new HashMap<>();
LinkedList<JsonNodeWrapper> queue = new LinkedList<>();
//Add root of json tree to Queue
JsonNodeWrapper root = new JsonNodeWrapper(node,"");
queue.offer(root);
while(queue.size()!=0){
JsonNodeWrapper curElement = queue.poll();
if(curElement.node.isObject()){
//Add all fields (JsonNodes) to the queue
Iterator<Map.Entry<String,JsonNode>> fieldIterator = curElement.node.fields();
while(fieldIterator.hasNext()){
Map.Entry<String,JsonNode> field = fieldIterator.next();
String prefix = (curElement.prefix==null || curElement.prefix.trim().length()==0)? "":curElement.prefix+".";
queue.offer(new JsonNodeWrapper(field.getValue(),prefix+field.getKey()));
}
}else if (curElement.node.isArray()){
//Add all array elements(JsonNodes) to the Queue
int i=0;
for(JsonNode arrayElement : curElement.node){
queue.offer(new JsonNodeWrapper(arrayElement,curElement.prefix+"["+i+"]"));
i++;
}
}else{
//If basic type, then time to fetch the Property value
jsonMap.put(curElement.prefix,curElement.node.asText());
System.out.println(curElement.prefix+"="+curElement.node.asText());
}
}
return jsonMap;
}
用法示例:
Map propsIterative = transformJsonToMapIterative(objectMapper.readTree(SAMPLE_JSON_DATA));
有固定级别的嵌套块吗?你自己尝试过吗?@krishnaPrasad没有,没有固定级别的嵌套块。它的dynamic@chrylis我试着使用jackson库,但我做不到。因此,发布不起作用的代码,并具体解释什么不起作用,而不仅仅是我不能。
//------------ Transform jackson JsonNode to Map -Iterative version -----------
public static Map<String,String> transformJsonToMapIterative(JsonNode node){
Map<String,String> jsonMap = new HashMap<>();
LinkedList<JsonNodeWrapper> queue = new LinkedList<>();
//Add root of json tree to Queue
JsonNodeWrapper root = new JsonNodeWrapper(node,"");
queue.offer(root);
while(queue.size()!=0){
JsonNodeWrapper curElement = queue.poll();
if(curElement.node.isObject()){
//Add all fields (JsonNodes) to the queue
Iterator<Map.Entry<String,JsonNode>> fieldIterator = curElement.node.fields();
while(fieldIterator.hasNext()){
Map.Entry<String,JsonNode> field = fieldIterator.next();
String prefix = (curElement.prefix==null || curElement.prefix.trim().length()==0)? "":curElement.prefix+".";
queue.offer(new JsonNodeWrapper(field.getValue(),prefix+field.getKey()));
}
}else if (curElement.node.isArray()){
//Add all array elements(JsonNodes) to the Queue
int i=0;
for(JsonNode arrayElement : curElement.node){
queue.offer(new JsonNodeWrapper(arrayElement,curElement.prefix+"["+i+"]"));
i++;
}
}else{
//If basic type, then time to fetch the Property value
jsonMap.put(curElement.prefix,curElement.node.asText());
System.out.println(curElement.prefix+"="+curElement.node.asText());
}
}
return jsonMap;
}
class JsonNodeWrapper{
public JsonNode node;
public String prefix;
public JsonNodeWrapper(JsonNode node, String prefix){
this.node = node;
this.prefix = prefix;
}
}
Map propsIterative = transformJsonToMapIterative(objectMapper.readTree(SAMPLE_JSON_DATA));