我想从 OMDB API 获取电影数据(JSON 文本)。我正在使用 Java 来解码它和 JSON-simple 包。
我想要解码的 URL 例如:http://www.omdbapi.com/?t=icarus
结果(直接复制粘贴,不结构化):
{"Title":"Icarus","Year":"2010","Rated":"TV-14","Released":"10 Dec 2010","Runtime":"42 min","Genre":"Adventure, Drama, Romance","Director":"Mairzee Almas","Writer":"Alfred Gough (creator), Jerry Siegel (character created by: Superman), Miles Millar (creator), Joe Shuster (character created by: Superman), Alfred Gough (developed for television by), Miles Millar (developed for television by), Genevieve Sparling","Actors":"Tom Welling, Erica Durance, Cassidy Freeman, Justin Hartley","Plot":"As the VRA threat intensifies, Clark takes initiative by closing down watchtower and declaring the League go officially underground, but will this be enough to stop trotter and Slade Wilson...","Language":"English","Country":"USA","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/M/MV5BMjIwNDQ2MjM5OV5BMl5BanBnXkFtZTcwODU4NzU0NA@@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"8.6","imdbVotes":"367","imdbID":"tt1628582","Type":"episode","Response":"True"}
我的按钮下的代码:
String url = "http://www.omdbapi.com/?t=" + jListFilms.getSelectedValue().toString();
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(url);
JSONObject jsonObj = (JSONObject) obj;
String title = (String) jsonObj.get("Title") ;
System.out.println(title);
} catch (ParseException e){
System.out.println(e);
}
当我打印出变量标题
Unexpected character (h) at position 0.
有谁知道为什么我看不到电影的标题吗?
最佳答案
您的代码所做的是解析以“http://”开头的字符串 URL,因此 h 位于位置 0。
您需要在该 URL 发出 HTTP GET 请求才能取回 JSON。
关于使用 Java 从 OMDB (IMDB) API 进行 Java JSON 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961194/