import org.json.JSONObject;
List<String> input = List.of("customer.name", "customer.phone", "shop.address", "nr");
JSONObject root = new JSONObject();
for (String s : input) {
// Split the string by the '.' character to get the keys
String[] keys = s.split("\\.");
JSONObject obj = root;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
if (i < keys.length - 1) {
if (!obj.has(key)) {
obj.put(key, new JSONObject());
}
obj = obj.getJSONObject(key);
} else {
obj.put(key, "");
}
}
}
1条答案
按热度按时间lsmd5eda1#
你可以这样做: