JSONArrary与JSONObject嵌套解析出错问题

JSONArrary与JSONObject嵌套解析出错问题


下面是我的解析过程:

public static Douban resolveJson(String jsonStr) throws JSONException {

    Douban douban = new Douban();
    JSONObject jsonObject = new JSONObject(jsonStr);
    int count = jsonObject.getInt("count");
    int total = jsonObject.getInt("total");
    if (count != 100 || total == 0) {
        douban = null;
        return douban;
    }
    douban.setCount(count);
    douban.setTotal(total);

    JSONArray jsonSubjects = jsonObject.getJSONArray("subjects");
    List<Subjects> subjectsList = new ArrayList<>();
    for (int i = 0; i < jsonSubjects.length(); i++) {
        JSONObject jsonSubjectsJSONObject = jsonSubjects.getJSONObject(i);
        Subjects subjects = new Subjects();

        JSONArray jsonGenres = jsonSubjectsJSONObject.getJSONArray("genres");
        List<String> genresList = new ArrayList<>();
        for (int j = 0; j < jsonGenres.length(); j++) {
            genresList.add(jsonGenres.getString(j));
        }
        subjects.setGenres(genresList);

        String title = jsonSubjectsJSONObject.getString("title");
        subjects.setTitle(title);

        JSONArray jsonCasts = jsonSubjectsJSONObject.getJSONArray("casts");
        List<Detail> detailCastsLists = new ArrayList<>();
        for (int j = 0; j < jsonCasts.length(); j++) {
            Detail detail = new Detail();

            JSONObject avatars = jsonCasts.getJSONObject(j).getJSONObject("avatars");
            detail.setSmall(avatars.getString("small"));

            detail.setName_en(jsonCasts.getJSONObject(j).getString("name_en"));
            detail.setName(jsonCasts.getJSONObject(j).getString("name"));
            detail.setAlt(jsonCasts.getJSONObject(j).getString("alt"));

            detailCastsLists.add(detail);
        }
        subjects.setCasts(detailCastsLists);

        JSONArray durations = jsonSubjectsJSONObject.getJSONArray("durations");
        for (int j = 0; j < durations.length(); j++) {
            subjects.setDurations(durations.getString(j));
        }

        subjects.setCollect_count(jsonSubjectsJSONObject.getLong("collect_count"));
        subjects.setMainland_pubdate(jsonSubjectsJSONObject.getString("mainland_pubdate"));
        subjects.setOriginal_title(jsonSubjectsJSONObject.getString("original_title"));

        JSONArray jsonDirectors = jsonSubjectsJSONObject.getJSONArray("directors");
        List<Detail> detailDirectorsList = new ArrayList<>();
        for (int j = 0; j < jsonDirectors.length(); j++) {
            Detail detail = new Detail();
            JSONObject jsonObject1 = jsonDirectors.getJSONObject(j);
            String avatars = jsonObject1.getString("avatars");
            JSONObject jsonObject2 = new JSONObject(avatars);
            detail.setSmall(jsonObject2.getString("small"));
            detail.setName_en(jsonObject1.getString("name_en"));
            detail.setName(jsonObject1.getString("name"));
            detail.setAlt(jsonObject1.getString("alt"));

            detailDirectorsList.add(detail);
        }
        subjects.setDirectors(detailDirectorsList);

        JSONArray jsonPubdates = jsonSubjectsJSONObject.getJSONArray("pubdates");
        List<String> pubdatesList = new ArrayList<>();
        for (int j = 0; j < jsonPubdates.length(); j++) {
            pubdatesList.add(jsonPubdates.getString(j));
        }
        subjects.setPubdates(pubdatesList);

        String year = jsonSubjectsJSONObject.getString("year");
        subjects.setYear(year);

        JSONObject jsonImages = jsonSubjectsJSONObject.getJSONObject("images");
        List<String> imageslist = new ArrayList<>();
        imageslist.add(jsonImages.getString("small"));
        imageslist.add(jsonImages.getString("large"));
        imageslist.add(jsonImages.getString("medium"));
        subjects.setImages(imageslist);

        subjects.setAlt(jsonSubjectsJSONObject.getString("alt"));
        subjectsList.add(subjects);
    }
    douban.setSubjects(subjectsList);
    douban.setTitle(jsonObject.getString("title"));
    return douban;
}

https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=北京&start=0&count=100&client=somemessage&udid=dddddddddddddddddddddd

下面是报错信息:
12-16 00:44:01.316 3919-3936/com.marliao.doubanfilm W/System.err: org.json.JSONException: Value null at avatars of type org.json.JSONObject$1 cannot be converted to JSONObject
12-16 00:44:01.326 3919-3936/com.marliao.doubanfilm W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
12-16 00:44:01.326 3919-3936/com.marliao.doubanfilm W/System.err: at org.json.JSONObject.getJSONObject(JSONObject.java:578)
12-16 00:44:01.326 3919-3936/com.marliao.doubanfilm W/System.err: at com.marliao.doubanfilm.Utils.ResolveJson.resolveJson(ResolveJson.java:76)
12-16 00:44:01.326 3919-3936/com.marliao.doubanfilm W/System.err: at com.marliao.doubanfilm.Activity.MainActivity$4.run(MainActivity.java:95)

问题还没解决,如果有大佬能看出问题,麻烦告知一下,在这里先拜谢了。
没有的话,我找到解决问的的方法后也会上传

猜你喜欢

转载自blog.csdn.net/qq_43094463/article/details/85028266
今日推荐