Javaを使ってwebページから情報抽出の勉強をしています。webページから情報を取ってきたいのですが、必要な情報がjson形式のため取得できません。htmlのようにパターン抽出などをすることができるのでしょうか。現在抜き出したいjsonをソースから抽出し、jsonicを使ってデコードしているのですがうまく変換できません。
<script type="application/ld+json"> [ { "@context" : "http://schema.org", "@type" : "Event", "name" : "LINE Presents 表参道イルミネーション2015", "startDate" : "2015-12-01", "endDate" : "2015-12-25", "location" : { "@type" : "Place", "address" : { "@type" : "PostalAddress", "addressLocality" : "神宮前", "addressRegion" : "東京" } },
java(ソースを抜き出し、変換する)
1package json; 2import java.io.BufferedReader; 3import java.io.IOException; 4import java.io.InputStream; 5import java.io.InputStreamReader; 6import java.net.HttpURLConnection; 7import java.net.URL; 8import java.util.regex.Matcher; 9import java.util.regex.Pattern; 10 11import net.arnx.jsonic.JSON; 12 13public class json { 14 15 public static void main(String args[]) throws IOException { 16 String title = ""; 17 URL url = new URL("http://www.jalan.net/event/130000/"); 18 HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 19 InputStream in = httpConn.getInputStream(); 20 BufferedReader reader = new BufferedReader(new InputStreamReader(in, 21 "SHIFT-JIS"));// JISAutoDetect 22 StringBuffer lines = new StringBuffer(4096); 23 while (true) { 24 String line = reader.readLine(); 25 if (line == null) { 26 break; 27 } 28 lines.append(line); 29 lines.append('\n'); 30 } 31 Pattern pattern = Pattern 32 .compile("<script type=.*?json.>(.*?)</script>", 33 Pattern.CASE_INSENSITIVE | Pattern.DOTALL); 34 Matcher matcher = pattern.matcher(lines.toString()); 35 while (matcher.find()) { 36 title = matcher.group(1); 37 System.out.println(title); 38 } 39 Event event = JSON.decode(title, Event.class); 40 System.out.println("name;" + event.getName()); 41 System.out.println("startDate;" + event.getStartDate()); 42 System.out.println("endDate:" + event.getEndDate()); 43 System.out.println("addressLocality:" + event.getAddressLocality()); 44 } 45}
Java(抜き出した値を保持するクラス)
1package json; 2 3public class Event { 4 5 String name; 6 String startDate; 7 String endDate; 8 String addressLocality; 9 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public String getStartDate() { 17 return startDate; 18 } 19 public void setStartDate(String startDate) { 20 this.startDate = startDate; 21 } 22 public String getEndDate() { 23 return endDate; 24 } 25 public void setEndDate(String endDate) { 26 this.endDate = endDate; 27 } 28 public String getAddressLocality() { 29 return addressLocality; 30 } 31 public void setAddressLocality(String addressLocality) { 32 this.addressLocality = addressLocality; 33 } 34 35} 36
エラーメッセージ
Exception in thread "main" net.arnx.jsonic.JSONException: [{@context=http://schema.org, @type=Event, name=LINE Presents 表参道イルミネーション2015, startDate=2015-12-01, endDate=2015-12-25, location={@type=Place, address={@type=PostalAddress, addressLocality=神宮前, addressRegion=東京}}, image=http://www.jalan.net/jalan/img/6/event/0176/KXL/e17658...... は class json.Event に変換できませんでした: $
at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1775)
at net.arnx.jsonic.JSON.parse(JSON.java:1155)
at net.arnx.jsonic.JSON.parse(JSON.java:1130)
at net.arnx.jsonic.JSON.decode(JSON.java:665)
at json.json.main(json.java:39)
Caused by: java.lang.UnsupportedOperationException: Cannot convert class java.util.ArrayList to class json.Event
at net.arnx.jsonic.ObjectConverter.convert(Converter.java:1655)
at net.arnx.jsonic.JSON.postparse(JSON.java:1310)
at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1762)
... 4 more
回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/24 01:45
2015/12/24 01:47