すべてのメタタグからog:description
、og:title
、book:release_date
、book:author
、およびの値を取得する方法を教えてください。book:isbn
(以下の JSON コンテンツを参照してください) 最終的にこれらの値はテーブルに表示されるので、Java オブジェクト (文字列) として必要です。
これまで、多くの外部ライブラリを使用してみましたが、これは JSON を使用する最初のプロジェクトなので、それらがどのように機能するのか理解できませんでした..
参考までに、私は NetBeans を使用しており、安らかな Web サービスを作成しています。
このコードを使用して、これらを JSON 文字列に変換できました。
JSONObject obj = new JSONObject(builder.toString());
String theJsonString = obj.toString();
しかし、私がこれをやろうとしたとき:
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> data = mapper.readValue(theJsonString.getBytes(), Map.class);
Map items = (Map) data.get("items");
Map pagemap = (Map) items.get("pagemap");
Map metatags = (Map) pagemap.get("metatags");
String b_title = (String) metatags.get("og:title");
System.out.println(b_title);
次のエラーが表示されます。
java.lang.ClassCastException: java.util.ArrayList を API.GCustSearchAPI.main(GCustSearchAPI.java:77) で java.util.Map にキャストできません
77行目は次のとおりです。
Map items = (Map) data.get("items");
ここにjsonの内容があります:
{
"items": [
{
"pagemap": {
"metatags": [
{
"og:description": "A boxed set, including the titles 'Harry Potter and the Philosopher's Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire', 'Harry Potter and the Order of the Phoenix', 'Harry Potter and the Half-Blood Prince' and 'Harry Potter and the Deathly Hallows'.",
"og:title": "Harry Potter Adult Paperback Boxed Set: Adult Edition (Paperback)",
"book:release_date": "2008-10-06",
"book:author": "J. K. Rowling",
"book:isbn": "9780747595847"
}
]
}
},
{
"pagemap": {
"metatags": [
{
"og:description": "Offers an in-depth look at the craftsmanship, artistry, technology, and more than ten-year journey that took the world's bestselling fiction from page to screen. From elaborate sets and luxurious costumes to advanced special effects and film making techniques, this title chronicles all eight films.",
"og:title": "Harry Potter: Page to Screen (Hardback)",
"book:release_date": "2011-10-25",
"book:author": "Bob McCabe",
"book:isbn": "9780857687753"
}
]
}
}
]
}
任意の入力をいただければ幸いです。初心者(NetBeansに触れて2ヶ月)なので簡単に教えてください。どうもありがとう!!!