完全在这里工作:
{"air_date":"(.*)-.*-.*","episodes|(?:episode_|\G)number":(.*?),
Java在行java.lang.NumberFormatException: null
episodios.add(...)
[...]
Matcher filter = Pattern.compile("\\{\"air_date\":\"(.*)-.*-.*\",\"episodes|(?:episode_|\\G)number\":(.*?),").matcher(response);
while (filter.find()) {
episodios.add(new Episodio(idSerie, temporada, Integer.parseInt(filter.group(2))));
}
}
响应是:http://pastebin.com/m4EJ1iP5
我知道它是JSON,而Regex并不是最佳选择。但它是必需的(老师)!
答案 0 :(得分:1)
正如您在debuggex正则表达式的第一场比赛中所看到的那样
{"air_date":"2009-03-08","episodes
所以你只匹配第1组(2009
- 03-08部分),但第2组是空的(代表null
),因为你将null
传递给{ {1}}哟正在获得Integer.parseInt
。确保您正在处理剧集匹配
NumberFormatException: null
测试第2组的值是否不像episode_number":1,
那样
null
但在现实世界中,你不应该用正则表达式来解析JSon,而是使用像GSon,Jackson那样适当的解析器。以下是如何使用GSon进行此操作的示例。
while (filter.find()) {
if (filter.group(1) != null) {
this.ano = Integer.parseInt(filter.group(1));
}
if (filter.group(2) != null) {
episodios.add(new Episodio(idSerie, temporada, Integer.parseInt(filter.group(2))));
}
}
输出:
String data = new Scanner(new File("input.txt")).useDelimiter("\\A").next();
Gson gson = new Gson();
Season season = gson.fromJson(data, Season.class);
//test
System.out.println(season.getName());
System.out.println("-------");
for (Episode ep : season.getEpisodes())
System.out.println(ep.getEpisode_number()+"\t"+ep.getName());
使用过的课程
Season 2
-------
1 Seven Thirty-Seven
2 Grilled
3 Bit by a Dead Bee
4 Down
5 Breakage
6 Peekaboo
7 Negro Y Azul
8 Better Call Saul
9 4 Days Out
10 Over
11 Mandala
12 Phoenix
13 ABQ
public class Episode {
private String air_date;
private Integer episode_number;
private String name;
private String overview;
private String still_path;
private Double vote_average;
private Integer vote_count;
//getters & setters
}