gpt4 book ai didi

json的java属性

转载 作者:IT老高 更新时间:2023-10-28 12:51:35 26 4
gpt4 key购买 nike

有没有一种简单的方法可以将带有点表示法的属性转换为 json

I.E

server.host=foo.bar
server.port=1234

{
"server": {
"host": "foo.bar",
"port": 1234
}
}

最佳答案

这不是简单的方法,但我设法使用 Gson 做到了。图书馆。结果将在 jsonBundle字符串。在这种情况下,我们在这里获取属性或 bundle :

final ResourceBundle bundle = ResourceBundle.getBundle("messages");
final Map<String, String> bundleMap = resourceBundleToMap(bundle);

final Type mapType = new TypeToken<Map<String, String>>(){}.getType();

final String jsonBundle = new GsonBuilder()
.registerTypeAdapter(mapType, new BundleMapSerializer())
.create()
.toJson(bundleMap, mapType);

对于这个实现 ResourceBundle必须转换为 Map包含 String作为键和String作为一个值。

private static Map<String, String> resourceBundleToMap(final ResourceBundle bundle) {
final Map<String, String> bundleMap = new HashMap<>();

for (String key: bundle.keySet()) {
final String value = bundle.getString(key);

bundleMap.put(key, value);
}

return bundleMap;
}

我必须创建自定义 JSONSerializer使用 Gson对于 Map<String, String> :

public class BundleMapSerializer implements JsonSerializer<Map<String, String>> {

private static final Logger LOGGER = LoggerFactory.getLogger(BundleMapSerializer.class);

@Override
public JsonElement serialize(final Map<String, String> bundleMap, final Type typeOfSrc, final JsonSerializationContext context) {
final JsonObject resultJson = new JsonObject();

for (final String key: bundleMap.keySet()) {
try {
createFromBundleKey(resultJson, key, bundleMap.get(key));
} catch (final IOException e) {
LOGGER.error("Bundle map serialization exception: ", e);
}
}

return resultJson;
}
}

这里是创建JSON的主要逻辑:

public static JsonObject createFromBundleKey(final JsonObject resultJson, final String key, final String value) throws IOException {
if (!key.contains(".")) {
resultJson.addProperty(key, value);

return resultJson;
}

final String currentKey = firstKey(key);
if (currentKey != null) {
final String subRightKey = key.substring(currentKey.length() + 1, key.length());
final JsonObject childJson = getJsonIfExists(resultJson, currentKey);

resultJson.add(currentKey, createFromBundleKey(childJson, subRightKey, value));
}

return resultJson;
}

private static String firstKey(final String fullKey) {
final String[] splittedKey = fullKey.split("\\.");

return (splittedKey.length != 0) ? splittedKey[0] : fullKey;
}

private static JsonObject getJsonIfExists(final JsonObject parent, final String key) {
if (parent == null) {
LOGGER.warn("Parent json parameter is null!");
return null;
}

if (parent.get(key) != null && !(parent.get(key) instanceof JsonObject)) {
throw new IllegalArgumentException("Invalid key \'" + key + "\' for parent: " + parent + "\nKey can not be JSON object and property or array in one time");
}

if (parent.getAsJsonObject(key) != null) {
return parent.getAsJsonObject(key);
} else {
return new JsonObject();
}
}

最后,如果有 key person.name.firstname有值 John , 会转换成这样的JSON :

{
"person" : {
"name" : {
"firstname" : "John"
}
}
}

希望这会有所帮助:)

关于json的java属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23871694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com