我正在尝试使用JSON Simple lib解析http://www.omdbapi.com提供的JSON电影信息(例如:http://www.omdbapi.com/?s=Lord%20Of%20the%20rings)但是没有成功。我不断收到错误org.json.simple.JSONObject无法强制转换为org.json.simple.JSONArray。
看看网站和休息时,我无法弄清楚为什么它无法解析信息。我使用下面提供的标准示例但没有成功。任何有用的帮助或替代JSON库都将受到赞赏。
是我从网站上获得的字符串:
JSONParser parser=new JSONParser();
Object obj = parser.parse(s);
JSONArray array = (JSONArray)obj;
答案 0 :(得分:1)
您从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
。
如果您需要内部数组,请使用get()
的{{1}}方法之一,使用键JSONObject
。