错误:类型org.json.JSONArray无法转换为JSONObject

时间:2019-05-02 09:06:13

标签: java android

我是Android Studio的新手,并且在解析JSON时遇到问题。 这是我的JSON:

List<WebElement> amtButton = driver.findElements(By.xpath("//select[@id='amt']"));
    if(amtButton.size()==1){
       amtButton.get(0).click();
    }  

我尝试这样解析它:

[{
        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"
      }]

我收到以下错误:

  

类型org.json.JSONArray无法转换为JSONObject

我知道我需要使用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); 而不是JsonArrayRequest,但是我不知道该如何获取对象? 我搜索了其他帖子,但听不懂!

您能帮我学习吗?

2 个答案:

答案 0 :(得分:1)

    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);

答案 1 :(得分:0)

您已经将response作为JSONArray

因此,您可以致电

JSONObject jresponse = response.getJSONObject(0);

onResponse内部分析您的回复。

JSONObject jresponse = response.getJSONObject(0);
String title= jresponse.getString("Title");
Log.d("title", title);

我希望这会对您有所帮助。