java - 错误:org.json.JSONArray 类型无法转换为 JSONObject
问题描述
我是 Android Studio 的新手,我在解析 JSON 时遇到问题。这是我的 JSON:
[{
id: 304,
Title: "Anna and the Apocalypse",
Year: "2017",
Released: "30 Nov 2018",
Runtime: "93 min",
Genre: "Comedy, Fantasy, Horror, Musical",
Director: "John McPhail",
Writer: "Alan McDonald, Ryan McHenry",
Actors: "Ella Hunt, Malcolm Cumming, Sarah Swire, Christopher Leveaux",
Language: "English",
Country: "UK",
Awards: "1 nomination.",
PosterLow: "http://movie30t.co/wp-content/themes/m30t/timthumb.php?src=http://movie30t.co/wp-content/uploads/2018/09/Anna-and-the-Apocalypse-2017.jpg&h=200%&w=133&zc=0",
PosterHigh: "http://movie30t.co/wp-content/themes/m30t/timthumb.php?src=http://movie30t.co/wp-content/uploads/2018/09/Anna-and-the-Apocalypse-2017.jpg&h=700%&w=500%&zc=0",
IMDBScore: "6.2",
Production: "Orion Pictures",
DL720pLink: "http://dl2.hostgig.ir/dl/f/Anna%20and%20the%20Apocalypse%202018/Anna%20and%20the%20Apocalypse%202018%20720p%20BRRip%20MOVIE30T.CO.mkv",
DL1080pLink: "N/A",
SubtitleLink: "N/A",
DLDubLink: "N/A",
State: "True",
Status: "Active",
ViewCount: "157",
VoteLike: "0",
VoteDisslike: "0",
TrailerLink: "http://dl2.hostgig.ir/dl/t/f/Anna and the Apocalypse 2018/Anna and the Apocalypse 2018.mp4",
Response: true,
Type: "Movie"
}]
我尝试像这样解析它:
RequestQueue queue = new Volley().newRequestQueue(this);
final JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Gson gson = new Gson();
MovieModel movieModel = gson.fromJson(response.toString(), MovieModel.class);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonArrayRequest);
我收到以下错误:
org.json.JSONArray 类型无法转换为 JSONObject
我知道我需要使用JsonArrayRequest
而不是,JsonArrayRequest
但我不知道如何获取对象呢?我搜索了一些其他帖子,但我不明白!
你能帮我学习一下吗?
解决方案
RequestQueue queue = new Volley().newRequestQueue(this);
final JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener< JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Gson gson = new Gson();
List<MovieModel> movieModels = gson.fromJson(response.toString(), List.class);
MovieModel movieModal = movieModels.get(0);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsonArrayRequest);
推荐阅读
- python - 使用 Python 请求下载文件时连接重置
- reactjs - 将图像置于文本之上
- c# - 找不到适用于应用程序的有效“aps-environment”权利字符串,Microsoft iOS 推送通知教程存在问题
- javascript - 如何在 Javascript 中增加 marginLeft onclick?
- c++ - C++:文件节拍进程运行时使用文件库写入文件的问题
- reactjs - Jest 和 Enzyme 配置无法正常运行我的 React/Typescript 项目
- java - 如何修复 Jar 文件的 JNI 问题?
- laravel - 此操作在 laravel 8 中未经授权
- python-3.x - sqlite3.OperationalError:列的默认 DATE 值不是常量
- blazor - 后端更改后 InputDate 字段未更新