public class ResultDemo {
/**
* 將 A.B.C 變成如下格式:
* {"A":{"B":{"C":"XXX"}}}
*/
public static void main(String args[]){
String module = "A";
String content = "XXX";
String result;
String[] split = module.split("\\.");
Map<String,Object> resultMap = new HashMap<>();
for (int i = 0; i < split.length; i++) {
if (i == split.length-1){
resultMap.put(split[i],content);
}else{
resultMap.put(split[i],new HashMap<>());
}
}
if (split.length > 1) {
for (int i = split.length-2; i >= 0 ; i--) {
Map<String,Object> tempMap = (HashMap)resultMap.get(split[i]);
tempMap.put(split[i+1],resultMap.get(split[i+1]));
}
Map<String,Object> newMap = new HashMap<>();
newMap.put(split[0],resultMap.get(split[0]));
result = new Gson().toJson(newMap);
} else {
result = new Gson().toJson(resultMap);
}
System.out.println(result);
}
}
Java Json 格式轉化 —— 將 A.B.C 轉成 {"A":{"B":{"C":"XXX"}}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.