相关疑难解决方法(0)

是否有JSON等效的XQuery/XPath?

在复杂的JSON数组和哈希中搜索项目时,例如:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]
Run Code Online (Sandbox Code Playgroud)

我可以用某种查询语言来查找项目in [0].objects where id = 3吗?

javascript xpath json xquery

213
推荐指数
14
解决办法
10万
查看次数

空检查链与捕获NullPointerException

Web服务返回一个巨大的XML,我需要访问它的深层嵌套字段.例如:

return wsObject.getFoo().getBar().getBaz().getInt()
Run Code Online (Sandbox Code Playgroud)

问题是getFoo(),getBar(),getBaz()可能所有的回报null.

但是,如果我null在所有情况下检查,代码将变得非常冗长且难以阅读.此外,我可能会错过某些领域的支票.

if (wsObject.getFoo() == null) return -1;
if (wsObject.getFoo().getBar() == null) return -1;
// maybe also do something with wsObject.getFoo().getBar()
if (wsObject.getFoo().getBar().getBaz() == null) return -1;
return wsObject.getFoo().getBar().getBaz().getInt();
Run Code Online (Sandbox Code Playgroud)

写作是否可以接受

try {
    return wsObject.getFoo().getBar().getBaz().getInt();
} catch (NullPointerException ignored) {
    return -1;
}
Run Code Online (Sandbox Code Playgroud)

还是会被视为反模式?

java null exception nullpointerexception custom-error-handling

108
推荐指数
4
解决办法
1万
查看次数