私は JsonPath を使用しており、提供されたパスが正しい場合、データを解析して値を取得できます。
ただし、間違ったパス (ユーザー入力) を指定すると、プログラムが終了します。私はそれが起こることを望んでいません。とにかく、この例外をキャッチして次のステップに進むことはできますか?
try
{
String value = JsonPath.read(jsonText, jsonPath);
System.out.println(value);
}
catch(InvalidPathException e)
{
System.out.println("ERROR<InvalidPathException>: "+e.getMessage());
}
無効なパスで次のエラーが発生します。
Exception in thread "main" com.jayway.jsonpath.InvalidPathException: invalid path
at com.jayway.jsonpath.internal.filter.FieldFilter.filter(FieldFilter.java:59)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:182)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
at office.jsonPathparse.main(jsonPathparse.java:37)
この例外は、jar のクラスである FieldFilter から発生しているようです。