【发布时间】:2021-03-01 08:21:15
【问题描述】:
如果存在特定值,我想读取一个 json 配置文件并执行代码。
{
"database": {
"port": 3306
}
}
我使用来自 maven Central 的 org.json。
if (jsonObject.has("database.port")) {
// Get content of "database.port" and call logic
}
默认情况下,database.port 不会被识别为嵌套路径。我如何告诉 org.json 这是一个嵌套路径?
if (jsonObject.has("database") && jsonObject.getJSONObject("database").has("port")
这工作得很好,但我有更多的嵌套值它变得相当混乱。
有没有更好的方法来获取内容而不是两次写入路径(JSONObject#has(...) 1x 和 JSONObject#getInt(...) 1x)
【问题讨论】:
-
JSONObject d=jsonObject.get("database");字符串端口=d.getString("端口");
-
@rupps 然后我必须使用
if (jsonObject.has("database") && jsonObject.getJSONObject("database").has("port"))这很不方便(特别是如果我有更多的嵌套值)。 -
你使用的json库是最基础的,不包含嵌套路径的nethods。