質問編集履歴
1
jsonicを使ってデコードをやってみました
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
Javaを使ってwebページから情報抽出の勉強をしています。webページから情報を取ってきたいのですが、必要な情報がjson形式のため取得できません。htmlのようにパターン抽出などをすることができるのでしょうか。
|
1
|
+
Javaを使ってwebページから情報抽出の勉強をしています。webページから情報を取ってきたいのですが、必要な情報がjson形式のため取得できません。htmlのようにパターン抽出などをすることができるのでしょうか。現在抜き出したいjsonをソースから抽出し、jsonicを使ってデコードしているのですがうまく変換できません。
|
2
|
+
```取得したいwebのソースの一部(http://www.jalan.net/event/130000/)
|
2
3
|
<script type="application/ld+json">
|
3
4
|
|
4
5
|
[
|
@@ -19,4 +20,103 @@
|
|
19
20
|
},
|
20
21
|
|
21
22
|
|
22
|
-
```
|
23
|
+
```
|
24
|
+
|
25
|
+
```java(ソースを抜き出し、変換する)
|
26
|
+
package json;
|
27
|
+
import java.io.BufferedReader;
|
28
|
+
import java.io.IOException;
|
29
|
+
import java.io.InputStream;
|
30
|
+
import java.io.InputStreamReader;
|
31
|
+
import java.net.HttpURLConnection;
|
32
|
+
import java.net.URL;
|
33
|
+
import java.util.regex.Matcher;
|
34
|
+
import java.util.regex.Pattern;
|
35
|
+
|
36
|
+
import net.arnx.jsonic.JSON;
|
37
|
+
|
38
|
+
public class json {
|
39
|
+
|
40
|
+
public static void main(String args[]) throws IOException {
|
41
|
+
String title = "";
|
42
|
+
URL url = new URL("http://www.jalan.net/event/130000/");
|
43
|
+
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
|
44
|
+
InputStream in = httpConn.getInputStream();
|
45
|
+
BufferedReader reader = new BufferedReader(new InputStreamReader(in,
|
46
|
+
"SHIFT-JIS"));// JISAutoDetect
|
47
|
+
StringBuffer lines = new StringBuffer(4096);
|
48
|
+
while (true) {
|
49
|
+
String line = reader.readLine();
|
50
|
+
if (line == null) {
|
51
|
+
break;
|
52
|
+
}
|
53
|
+
lines.append(line);
|
54
|
+
lines.append('\n');
|
55
|
+
}
|
56
|
+
Pattern pattern = Pattern
|
57
|
+
.compile("<script type=.*?json.>(.*?)</script>",
|
58
|
+
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
|
59
|
+
Matcher matcher = pattern.matcher(lines.toString());
|
60
|
+
while (matcher.find()) {
|
61
|
+
title = matcher.group(1);
|
62
|
+
System.out.println(title);
|
63
|
+
}
|
64
|
+
Event event = JSON.decode(title, Event.class);
|
65
|
+
System.out.println("name;" + event.getName());
|
66
|
+
System.out.println("startDate;" + event.getStartDate());
|
67
|
+
System.out.println("endDate:" + event.getEndDate());
|
68
|
+
System.out.println("addressLocality:" + event.getAddressLocality());
|
69
|
+
}
|
70
|
+
}
|
71
|
+
```
|
72
|
+
|
73
|
+
```Java(抜き出した値を保持するクラス)
|
74
|
+
package json;
|
75
|
+
|
76
|
+
public class Event {
|
77
|
+
|
78
|
+
String name;
|
79
|
+
String startDate;
|
80
|
+
String endDate;
|
81
|
+
String addressLocality;
|
82
|
+
|
83
|
+
public String getName() {
|
84
|
+
return name;
|
85
|
+
}
|
86
|
+
public void setName(String name) {
|
87
|
+
this.name = name;
|
88
|
+
}
|
89
|
+
public String getStartDate() {
|
90
|
+
return startDate;
|
91
|
+
}
|
92
|
+
public void setStartDate(String startDate) {
|
93
|
+
this.startDate = startDate;
|
94
|
+
}
|
95
|
+
public String getEndDate() {
|
96
|
+
return endDate;
|
97
|
+
}
|
98
|
+
public void setEndDate(String endDate) {
|
99
|
+
this.endDate = endDate;
|
100
|
+
}
|
101
|
+
public String getAddressLocality() {
|
102
|
+
return addressLocality;
|
103
|
+
}
|
104
|
+
public void setAddressLocality(String addressLocality) {
|
105
|
+
this.addressLocality = addressLocality;
|
106
|
+
}
|
107
|
+
|
108
|
+
}
|
109
|
+
|
110
|
+
```
|
111
|
+
エラーメッセージ
|
112
|
+
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 に変換できませんでした: $
|
113
|
+
at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1775)
|
114
|
+
at net.arnx.jsonic.JSON.parse(JSON.java:1155)
|
115
|
+
at net.arnx.jsonic.JSON.parse(JSON.java:1130)
|
116
|
+
at net.arnx.jsonic.JSON.decode(JSON.java:665)
|
117
|
+
at json.json.main(json.java:39)
|
118
|
+
Caused by: java.lang.UnsupportedOperationException: Cannot convert class java.util.ArrayList to class json.Event
|
119
|
+
at net.arnx.jsonic.ObjectConverter.convert(Converter.java:1655)
|
120
|
+
at net.arnx.jsonic.JSON.postparse(JSON.java:1310)
|
121
|
+
at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1762)
|
122
|
+
... 4 more
|