天天看點

六、常用工具之Jackson

{

    "rating": {
        "max": 10,
        "average": 8.4,
        "stars": "45",
        "min": 0
    },
    "reviews_count": 5556,
    "wish_count": 131272,
    "douban_site": "",
    "year": "2016",
    "images": {
        "small": "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2395733377.jpg",
        "large": "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2395733377.jpg",
        "medium": "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2395733377.jpg"
    },
    "alt": "https://movie.douban.com/subject/26683290/",
    "id": "26683290",
    "mobile_url": "https://movie.douban.com/subject/26683290/mobile",
    "title": "你的名字。",
    "do_count": null,
    "share_url": "http://m.douban.com/movie/subject/26683290",
    "seasons_count": null,
    "schedule_url": "",
    "episodes_count": null,
    "countries": [
        "日本"
    ],
    "genres": [
        "劇情",
        "愛情",
        "動畫"
    ],
    "collect_count": 871951,
    "casts": [
        {
            "alt": "https://movie.douban.com/celebrity/1185637/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg"
            },
            "name": "神木隆之介",
            "id": "1185637"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1316660/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg"
            },
            "name": "上白石萌音",
            "id": "1316660"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1018667/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg"
            },
            "name": "長澤雅美",
            "id": "1018667"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1008549/",
            "avatars": {
                "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg",
                "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg",
                "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg"
            },
            "name": "市原悅子",
            "id": "1008549"
        }
    ],
    "current_season": null,
    "original_title": "君の名は。",
    "summary": "在遠離大都會的小山村,住着巫女世家出身的高中女孩宮水三葉(上白石萌音 配音)。校園和家庭的原因本就讓她充滿煩惱,而近一段時間發生的奇怪事件,又讓三葉摸不清頭腦。不知從何時起,三葉在夢中就會變成一個住在東京的高中男孩。那裡有陌生的同學和朋友,有親切的前輩和繁華的街道,一切都是如此誘人而真實。另一方面,住在東京的高中男孩立花泷(神木隆之介 配音)則總在夢裡來到陌生的小山村,以女孩子的身份過着全新的生活。許是受那顆神秘彗星的影響,立花和三葉在夢中交換了身份。他們以他者的角度體驗着對方的人生,這期間有憤怒、有歡笑也有暖心。隻是兩人并不知道,身份交換的背後隐藏着重大而錐心的秘密……\n本片為2016年度日本本土影片票房冠軍。©豆瓣",
    "subtype": "movie",
    "directors": [
        {
            "alt": "https://movie.douban.com/celebrity/1005177/",
            "avatars": {
                "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44681.jpg",
                "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44681.jpg",
                "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44681.jpg"
            },
            "name": "新海誠",
            "id": "1005177"
        }
    ],
    "comments_count": 188184,
    "ratings_count": 627622,
    "aka": [
        "你的名字",
        "君之名",
        "Your Name",
        "Kimi no na wa."
    ]

}

           

解析如上複雜json:

private MovieDto getMovieDetailByUrl(String url){
        MovieDto movieDto = new MovieDto();
        ObjectMapper mapper = new ObjectMapper();
        String responseBody = http(url);
        if (!StringUtils.isEmpty(url)){
            JsonNode movieNode = null;
            try {
                movieNode = mapper.readTree(responseBody);
                movieDto.setTitle(movieNode.path("title").asText());
                movieDto.setCoverImg(movieNode.path("images").path("large").asText());
                movieDto.setAverage(movieNode.path("rating").path("average").asDouble());
                movieDto.setStars(movieNode.path("rating").path("stars").asInt());

                movieDto.setCountry(processArray(movieNode.get("countries")));
                movieDto.setOriginalTitle(movieNode.path("original_title").asText());
                movieDto.setWishCount(movieNode.path("wish_count").asInt());
                movieDto.setCommentCount(movieNode.path("comments_count").asInt());
                movieDto.setYear(movieNode.path("year").asText());

                movieDto.setGenres(processArray(movieNode.get("genres")));

                List<Person> directors = getPersons(movieNode.get("directors"));
                movieDto.setDirectors(directors);

                List<Person> casts = getPersons(movieNode.get("casts"));
                movieDto.setCasts(casts);

                movieDto.setSummary(movieNode.path("summary").asText());

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return movieDto;
    }
           

關鍵點一:

  對于節點中嵌套多個對象,此時已經無法通過

movieNode.path("casts")

來擷取節點了,而需要通過

movieNode.get("casts")

擷取節點,比如:

"casts": [
        {
            "alt": "https://movie.douban.com/celebrity/1185637/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13768.jpg"
            },
            "name": "神木隆之介",
            "id": "1185637"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1316660/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1445093052.07.jpg"
            },
            "name": "上白石萌音",
            "id": "1316660"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1018667/",
            "avatars": {
                "small": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg",
                "large": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg",
                "medium": "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1483767998.76.jpg"
            },
            "name": "長澤雅美",
            "id": "1018667"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1008549/",
            "avatars": {
                "small": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg",
                "large": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg",
                "medium": "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p40111.jpg"
            },
            "name": "市原悅子",
            "id": "1008549"
        }
    ]