質問編集履歴

1

jsonicを使ってデコードをやってみました

2015/12/22 01:32

投稿

ryo-flat
ryo-flat

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
- Javaを使ってwebページから情報抽出の勉強をしています。webページから情報を取ってきたいのですが、必要な情報がjson形式のため取得できません。htmlのようにパターン抽出などをすることができるのでしょうか。```取得したいwebのソースの一部(http://www.jalan.net/event/130000/)
1
+ Javaを使ってwebページから情報抽出の勉強をしています。webページから情報を取ってきたいのですが、必要な情報がjson形式のため取得できません。htmlのようにパターン抽出などをすることができるのでしょうか。現在抜き出したいjsonをソースから抽出し、jsonicを使ってデコードしているのですがうまく変換できません。
2
+
3
+ ```取得したいwebのソースの一部(http://www.jalan.net/event/130000/)
2
4
 
3
5
  <script type="application/ld+json">
4
6
 
@@ -41,3 +43,201 @@
41
43
 
42
44
 
43
45
  ```
46
+
47
+
48
+
49
+ ```java(ソースを抜き出し、変換する)
50
+
51
+ package json;
52
+
53
+ import java.io.BufferedReader;
54
+
55
+ import java.io.IOException;
56
+
57
+ import java.io.InputStream;
58
+
59
+ import java.io.InputStreamReader;
60
+
61
+ import java.net.HttpURLConnection;
62
+
63
+ import java.net.URL;
64
+
65
+ import java.util.regex.Matcher;
66
+
67
+ import java.util.regex.Pattern;
68
+
69
+
70
+
71
+ import net.arnx.jsonic.JSON;
72
+
73
+
74
+
75
+ public class json {
76
+
77
+
78
+
79
+ public static void main(String args[]) throws IOException {
80
+
81
+ String title = "";
82
+
83
+ URL url = new URL("http://www.jalan.net/event/130000/");
84
+
85
+ HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
86
+
87
+ InputStream in = httpConn.getInputStream();
88
+
89
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in,
90
+
91
+ "SHIFT-JIS"));// JISAutoDetect
92
+
93
+ StringBuffer lines = new StringBuffer(4096);
94
+
95
+ while (true) {
96
+
97
+ String line = reader.readLine();
98
+
99
+ if (line == null) {
100
+
101
+ break;
102
+
103
+ }
104
+
105
+ lines.append(line);
106
+
107
+ lines.append('\n');
108
+
109
+ }
110
+
111
+ Pattern pattern = Pattern
112
+
113
+ .compile("<script type=.*?json.>(.*?)</script>",
114
+
115
+ Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
116
+
117
+ Matcher matcher = pattern.matcher(lines.toString());
118
+
119
+ while (matcher.find()) {
120
+
121
+ title = matcher.group(1);
122
+
123
+ System.out.println(title);
124
+
125
+ }
126
+
127
+ Event event = JSON.decode(title, Event.class);
128
+
129
+ System.out.println("name;" + event.getName());
130
+
131
+ System.out.println("startDate;" + event.getStartDate());
132
+
133
+ System.out.println("endDate:" + event.getEndDate());
134
+
135
+ System.out.println("addressLocality:" + event.getAddressLocality());
136
+
137
+ }
138
+
139
+ }
140
+
141
+ ```
142
+
143
+
144
+
145
+ ```Java(抜き出した値を保持するクラス)
146
+
147
+ package json;
148
+
149
+
150
+
151
+ public class Event {
152
+
153
+
154
+
155
+ String name;
156
+
157
+ String startDate;
158
+
159
+ String endDate;
160
+
161
+ String addressLocality;
162
+
163
+
164
+
165
+ public String getName() {
166
+
167
+ return name;
168
+
169
+ }
170
+
171
+ public void setName(String name) {
172
+
173
+ this.name = name;
174
+
175
+ }
176
+
177
+ public String getStartDate() {
178
+
179
+ return startDate;
180
+
181
+ }
182
+
183
+ public void setStartDate(String startDate) {
184
+
185
+ this.startDate = startDate;
186
+
187
+ }
188
+
189
+ public String getEndDate() {
190
+
191
+ return endDate;
192
+
193
+ }
194
+
195
+ public void setEndDate(String endDate) {
196
+
197
+ this.endDate = endDate;
198
+
199
+ }
200
+
201
+ public String getAddressLocality() {
202
+
203
+ return addressLocality;
204
+
205
+ }
206
+
207
+ public void setAddressLocality(String addressLocality) {
208
+
209
+ this.addressLocality = addressLocality;
210
+
211
+ }
212
+
213
+
214
+
215
+ }
216
+
217
+
218
+
219
+ ```
220
+
221
+ エラーメッセージ
222
+
223
+ 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 に変換できませんでした: $
224
+
225
+ at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1775)
226
+
227
+ at net.arnx.jsonic.JSON.parse(JSON.java:1155)
228
+
229
+ at net.arnx.jsonic.JSON.parse(JSON.java:1130)
230
+
231
+ at net.arnx.jsonic.JSON.decode(JSON.java:665)
232
+
233
+ at json.json.main(json.java:39)
234
+
235
+ Caused by: java.lang.UnsupportedOperationException: Cannot convert class java.util.ArrayList to class json.Event
236
+
237
+ at net.arnx.jsonic.ObjectConverter.convert(Converter.java:1655)
238
+
239
+ at net.arnx.jsonic.JSON.postparse(JSON.java:1310)
240
+
241
+ at net.arnx.jsonic.JSON$Context.convertInternal(JSON.java:1762)
242
+
243
+ ... 4 more