gpt4 book ai didi

java - 解析 IMDB-API JSON

转载 作者:行者123 更新时间:2023-12-01 14:08:30 26 4
gpt4 key购买 nike

我正在尝试解析 http://www.omdbapi.com 提供的 JSON 电影信息(示例: http://www.omdbapi.com/?s=Lord%20Of%20the%20rings )使用 JSON Simple lib 但没有成功。我不断收到错误 org.json.simple.JSONObject 无法转换为 org.json.simple.JSONArray。

查看该网站并查看其休息情况,我无法弄清楚为什么它无法解析信息。我正在使用下面提供的标准示例,但没有成功。任何可以使用的帮助或替代 JSON 库将不胜感激。

s 是我从网站获得的字符串:

JSONParser parser=new JSONParser();
Object obj = parser.parse(s);
JSONArray array = (JSONArray)obj;

最佳答案

您从 API 检索的 JSON 为

{"Search":[{"Title":"The Lord of the Rings: The Fellowship of the Ring","Year":"2001","imdbID":"tt0120737","Type":"movie"},{"Title":"The Lord of the Rings: The Return of the King","Year":"2003","imdbID":"tt0167260","Type":"movie"},{"Title":"The Lord of the Rings: The Two Towers","Year":"2002","imdbID":"tt0167261","Type":"movie"},{"Title":"The Lord of the Rings","Year":"1978","imdbID":"tt0077869","Type":"movie"},{"Title":"The Lord of the Rings: The Two Towers","Year":"2002","imdbID":"tt0347436","Type":"game"},{"Title":"The Lord of the Rings: The Return of the King","Year":"2003","imdbID":"tt0387360","Type":"game"},{"Title":"The Lord of the Rings: The Battle for Middle-Earth","Year":"2004","imdbID":"tt0412935","Type":"game"},{"Title":"Lord of the Rings: Battle for Middle Earth II - Rise of the Witch King","Year":"2006","imdbID":"tt1058040","Type":"game"},{"Title":"The Lord of the Rings: The Battle for Middle-Earth II","Year":"2006","imdbID":"tt0760172","Type":"game"},{"Title":"The Lord of the Rings: The Third Age","Year":"2004","imdbID":"tt0415947","Type":"game"}]}

{开头。这是一个 JSON 对象 (JSONObject),而不是一个数组 (JSONArray)。

Object obj = parser.parse(s);

Object obj 的动态类型将为 JSONObject,无法转换为 JSONArray

如果您想要内部数组,请使用 JSONObjectget() 方法之一,并使用键 "Search"

关于java - 解析 IMDB-API JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18725201/

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