以下の様にHttpURLConnectionで帰ってきたGoogleVisionのJSONデータをString型respに入れた後、
[2]や[3]にて全体のデータは取れているのですが、それより下位のデータにアクセスする事ができません。
どの様にすればアクセスできるのでしょうか。
今回JSONデータを初めて扱うので何が間違ってるのかどうすれば良いのか解らない状態で行き詰ってます。
ご教示のほどよろしくお願いします。
目的:
以下JSONデータの
responses > webDetection > pagesWithMatchingImages > urlのデータを取得したい
コード: //URLConnection urlConnection = serverUrl.openConnection(); //HttpURLConnection httpConnection = (HttpURLConnection)urlConnection; Scanner httpResponseScanner = new Scanner (httpConnection.getInputStream()); String resp=""; while (httpResponseScanner.hasNext()) { String line = httpResponseScanner.nextLine(); resp += line; } // [1] //JsonNode root = mapper.readTree(resp); //String a4 = root.get("responses").get("webDetection").get("pagesWithMatchingImages").get(0).get("url").asText(); //→エラー:An exception occured while executing the Java class. null // [2] ObjectMapper mapper = new ObjectMapper(); Map<String,Object> result = mapper.readValue(resp, Map.class); System.out.println("result.toString() : " + result toString()); // [3] ObjectMapper mapper = new ObjectMapper(); Map<String,Object> result = mapper.readValue(resp, Map.class); System.out.println("result:" + result.get("responses").getClass() +":"+result.get("responses"));
// [2]の結果 //※冗長部分は省いて、見やすい様に改行を付けています。 { responses= [ { webDetection= { webEntities= [ { entityId=<文字列>, score=<数値> }, { entityId=<文字列>, score=<数値> }, ], pagesWithMatchingImages= [ { url=https://・・・ }, { url=https://・・・ } ] } } ] }
// [3]の結果 // ※冗長部分は省いて、見やすい様に改行を付けています。 [ { webDetection= { webEntities= [ { entityId=<文字列>, score=<数値> }, { entityId=<文字列>, score=<数値> }, ], pagesWithMatchingImages= [ { url=https://・・・ }, { url=https://・・・ } ] } } ]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。