AFAIK,没有知名的图书馆可以做到这一点。但是你可以用一种简单的方式做到这一点:
public String propertiesToJson(Properties p) {
Map tree = new LinkedHashMap();
for (String name : p.stringPropertyNames()) {
String[] parts = name.split("\\.");
Map nextTree = tree;
for (int i = 0, partsLength = parts.length; i < partsLength; i++) {
String part = parts[i];
Object v = nextTree.get(part);
if (v == null) {
if (i < partsLength - 1) {
Map newNextTree = new LinkedHashMap();
nextTree.put(part, newNextTree);
nextTree = newNextTree;
} else {
nextTree.put(part, p.getProperty(name));
}
} else {
if (i < partsLength - 1) {
nextTree = (Map) v;
}
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("Properties : {\n");
recursive(tree, sb, 1);
sb.append("}");
return sb.toString();
}
private void recursive(Map tree, StringBuilder sb, int deep) {
boolean first = true;
for (Object key : tree.keySet()) {
if (!first) sb.append(",\n");
else first = false;
for (int t = 0; t < deep; t++) sb.append("\t");
sb.append(key + " : ");
Object v = tree.get(key);
if (v instanceof Map) {
sb.append("{\n");
recursive((Map) v, sb, deep+1);
for (int t = 0; t < deep; t++) sb.append("\t");
sb.append("}");
} else {
sb.append(v);
}
}
sb.append("\n");
}