0

私は 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 から発生しているようです。

4

2 に答える 2

0

ライブラリは、以下のコードに基づいて無効なパス例外を発生させます(taken from grepcode)

if (jsonPath == null || jsonPath.trim().isEmpty() || jsonPath.matches("new ") 
       || jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {

       throw new InvalidPathException("Invalid path");

 }

したがって、jsonPathを出力して上記の条件と一致させる必要があると思います。

于 2013-08-30T19:57:08.397 に答える