问题描述
{
"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)
解决方法
正如rupps所说,org.json不支持嵌套路径。我升级到支持更高级解析的 gson。 JsonPath 看起来不错,但对于我的要求来说似乎有点矫枉过正。
,抱歉,org.json 不支持嵌套解析。不过你可以试试把json转成bean,会容易很多