gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:07:31 26 4
gpt4 key购买 nike

我想从 JSON 文本的 OMDB API 获取电影数据。我正在使用 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);
}

当我打印出变量title

Unexpected character (h) at position 0.

有谁知道为什么我看不到电影的名字?

最佳答案

您的代码所做的是解析以“http://”开头的字符串 URL,因此 h 位于位置 0。

您需要在该网址发出 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com