使用 Java 从 OMDB (IMDB) API 进行 Java JSON 解码

标签 java api imdb json-simple

我想从 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。

Here's an answer for how to issue the GET request

关于使用 Java 从 OMDB (IMDB) API 进行 Java JSON 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23961194/

相关文章:

使用异常的 Java 类

javascript - 保护 Web api 不被浏览器外部调用

javascript - 图像未加载 javascript、html、angular、imdb api

python - imdbpy "nr_order"表中的 "cast_info"列代表什么?

java - 从 MySQL 添加行时更改 JTable 的整个行颜色

java - Spring 错误: only capable of generating responses with characteristics not acceptable according to the request "accept" headers

java数组显示最低学生

node.js - 如何在node js中的api(计算机视觉-azure中的缩略图服务)响应的网页中显示图像

excel - 使用 DeepL API 翻译文本

web-services - 连接到 IMDB