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

使用Java从OMDB(IMDB)API解码Java JSON,java,api,imdb,json-simple,Java,Api,Imdb,Json Simple,我想从OMDB API获取电影数据,它是JSON文本。我使用Java来解码这个和JSON simple包 我要解码的URL如下所示,例如: 结果(直接复制和粘贴,非结构化): {“标题”:“伊卡洛斯”,“年份”:“2010”,“评级”:“电视-14”,“发布”:“2010年12月10日”,“运行时”:“42分钟”,“流派”:“冒险、戏剧、浪漫”,“导演”:“迈尔泽伊·阿尔马斯”,“作家”:“阿尔弗雷德·高夫(创作者)”,杰里·西格尔(创作者:超人),迈尔斯·米勒(创作者),乔·舒斯特(创作者:超

我想从OMDB API获取电影数据,它是JSON文本。我使用Java来解码这个和JSON simple包

我要解码的URL如下所示,例如:

结果(直接复制和粘贴,非结构化):

{“标题”:“伊卡洛斯”,“年份”:“2010”,“评级”:“电视-14”,“发布”:“2010年12月10日”,“运行时”:“42分钟”,“流派”:“冒险、戏剧、浪漫”,“导演”:“迈尔泽伊·阿尔马斯”,“作家”:“阿尔弗雷德·高夫(创作者)”,杰里·西格尔(创作者:超人),迈尔斯·米勒(创作者),乔·舒斯特(创作者:超人),阿尔弗雷德·高夫(为电视而开发)、迈尔斯·米勒(为电视而开发)、热纳维夫·斯帕林、“演员”:“汤姆·韦林、艾丽卡·杜兰斯、卡西迪·弗里曼、贾斯汀·哈特利”、“情节”:“随着VRA威胁的加剧,克拉克主动关闭了望塔,宣布联盟正式地下化,但这是否足以阻止特罗特和斯莱德·威尔逊……”,“语言”:“英语”,“国家”:“美国”,“奖项”:“N/A”,“海报”:“Metascore”:“N/A”,“imdbRating”:“8.6”,“IMDBVoces”:“367”,“imdbID”:“tt1628582”,“类型”:”插曲“,”回应“:“真实”}

“我的”按钮下的代码:

        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);
    }
当我打印变量标题时

位置0处的意外字符(h)


有人知道我为什么没有得到电影的标题吗?

您的代码正在解析以“http://”开头的字符串URL,因此是位置0处的h

您需要在该url上发出HTTP GET请求,以获取JSON


感谢Adam带领我走向正确的方向。我现在使用的是InputStream和Google Gson,而不是JSON。现在简单了

这是我现在的代码,它按照我希望的方式工作

        try {

        String selectedItem = jListFilms.getSelectedValue().toString().replace("\\s+", "+");

        InputStream input = new URL("http://www.omdbapi.com/?t=" + URLEncoder.encode(selectedItem, "UTF-8")).openStream();
        Map<String, String> map = new Gson().fromJson(new InputStreamReader(input, "UTF-8"), new TypeToken<Map<String, String>>(){}.getType());

        String title = map.get("Title");
        String year = map.get("Year");
        String released = map.get("Released");
        String runtime = map.get("Runtime");
        String genre = map.get("Genre");
        String actors = map.get("Actors");
        String plot = map.get("Plot");
        String imdbRating = map.get("imdbRating");
        String poster = map.get("Poster");

        testForm tf = new testForm(title, year, released, runtime, genre, actors, plot, imdbRating);
        tf.setVisible(true);

    } catch (JsonIOException | JsonSyntaxException | IOException e){
        System.out.println(e);
    }
试试看{
字符串selectedItem=jListFilms.getSelectedValue().toString().replace(“\\s+”,“+”);
InputStream输入=新URL(“http://www.omdbapi.com/?t=“+urlcoder.encode(selectedItem,“UTF-8”)).openStream();
Map Map=new Gson().fromJson(新的InputStreamReader(输入,“UTF-8”),新的TypeToken(){}.getType());
字符串title=map.get(“title”);
字符串year=map.get(“年”);
String released=map.get(“released”);
字符串runtime=map.get(“runtime”);
字符串类型=map.get(“类型”);
String actors=map.get(“actors”);
字符串plot=map.get(“plot”);
字符串imdbRating=map.get(“imdbRating”);
字符串poster=map.get(“poster”);
testForm tf=新的testForm(标题、年份、发布、运行时、流派、演员、情节、imdbRating);
tf.setVisible(真);
}捕获(JsonIOException | JsonSyntaxException | IOE异常){
系统输出打印ln(e);
}