首页 > 解决方案 > 错误: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但我不知道如何获取对象呢?我搜索了一些其他帖子,但我不明白!

你能帮我学习一下吗?

标签: javaandroid

解决方案


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

推荐阅读