Java 错误:无法将类型org.json.JSONArray转换为JSONObject
我是androidstudio的新手&我在解析JSON时遇到了一个问题。 这是我的JSON:Java 错误:无法将类型org.json.JSONArray转换为JSONObject,java,android,Java,Android,我是androidstudio的新手&我在解析JSON时遇到了一个问题。 这是我的JSON: [{ id: 304, Title: "Anna and the Apocalypse", Year: "2017", Released: "30 Nov 2018", Runtime: "93 min", Genre: "Comedy, Fantasy, Horror, Musical",
[{
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);
RequestQueue queue=new-Volley().newRequestQueue(这个);
final JsonObjectRequest jsonArrayRequest=新JsonObjectRequest(Request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Gson Gson=新的Gson();
MovieModel MovieModel=gson.fromJson(response.toString(),MovieModel.class);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_SHORT.show();
}
});
add(jsonArrayRequest);
我得到以下错误:
无法将类型org.json.JSONArray转换为JSONObject
我知道我需要使用JsonArrayRequest
而不是JsonArrayRequest
,但我不知道如何获取对象?
我搜索了其他一些帖子,但我不明白
你能帮我学习一下吗?RequestQueue queue=new-Volley().newRequestQueue(这个);
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);
final JsonObjectRequest jsonArrayRequest=新JsonObjectRequest(Request.Method.GET,url,null,
新响应。侦听器(){
@凌驾
公共void onResponse(JSONArray响应){
Gson Gson=新的Gson();
List movieModels=gson.fromJson(response.toString(),List.class);
MovieModel movieModal=movieModels.get(0);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_SHORT.show();
}
});
add(jsonArrayRequest);
您已经以JSONArray
的形式收到了响应
所以,你可以打电话
JSONObject jresponse = response.getJSONObject(0);
内部onResponse
解析您的响应
JSONObject jresponse = response.getJSONObject(0);
String title= jresponse.getString("Title");
Log.d("title", title);
我希望这会对您有所帮助。因为您尝试解析的JSON不是JSONObject,而是JSONArray,如果您想解析JSONObject,请尝试删除JSONObject开头和结尾的“[”“]”JSON@vc73我从API获得这个Json,我无法访问它!在这种情况下,尝试解析JSONArray而不是使用gson的任何原因?可以简单地用JSONArray/JSONObject解析它。