从JSON获取图像URL

时间:2014-10-12 22:04:08

标签: java android json

我是JSON的新手,所以我对此有疑问。

我想在JSONObject信息中获取图像JSON对象。 但是最好的方法是什么?

当我收到JSON请求后,我得到了这个:

{
    "status" : "done",
    "info" : {
        "rating" : {
            "imdb" : [5.7000000000000002, 129331]
        },
        "genres" : ["Action", "Crime", "Thriller"],
        "tmdb_id" : 584,
        "plot" : "It's a major double-cross when former police officer Brian O'Conner teams up with his ex-con buddy Roman Pearce to transport a shipment of \"dirty\" money for shady Miami-based import-export dealer Carter Verone. But the guys are actually working with undercover agent Monica Fuentes to bring Verone down.",
        "tagline" : "How Fast Do You Want It?",
        "original_title" : "2 Fast 2 Furious",
        "actor_roles" : {
            "Mark Boone Junior" : "Detective Whitworth",
            "Amaury Nolasco" : "Orange Julius",
            "Edward Finlay" : "Agent Dunn",
            "Cole Hauser" : "Carter Verone",
            "Ludacris" : "Tej",
            "Devon Aoki" : "Suki",
            "James Remar" : "Agent Markham",
            "Jin Auyeung" : "Jimmy",
            "Eric Etebari" : "Darden",
            "Paul Walker" : "Brian O'Conner",
            "Mo Gallini" : "Enrique",
            "Eva Mendes" : "Monica Fuentes",
            "Tyrese Gibson" : "Roman Pearce",
            "Thom Barry" : "Agent Bilkins",
            "Michael Ealy" : "Slap Jack"
        },
        "collection" : "The Fast and the Furious Collection",
        "via_imdb" : true,
        "mpaa" : "PG-13",
        "via_tmdb" : true,
        "directors" : ["John Singleton"],
        "titles" : ["2 Fast 2 Furious", "The Fast and the Furious 2", "M\u00e1s r\u00e1pido, m\u00e1s furioso", "\u73a9\u547d\u95dc\u982d2 \u98c6\u98a8\u518d\u8d77", "Fast and the Furious Vol2", "The Fast and the Furious 2 - 2 Fast 2 Furious", "+ Veloses + Furiosos", "Rapide et dangereux 2", "Fast & Furious 2", "Fast and Furious 2", "\ubd84\ub178\uc758 \uc9c8\uc8fc 2"],
        "imdb" : "tt0322259",
        "year" : 2003,
        "images" : {
            "disc_art" : ["http://assets.fanart.tv/fanart/movies/584/moviedisc/2-fast-2-furious-520e18cde06ab.png"],
            "poster" : ["https://image.tmdb.org/t/p/w154/3CrNPUulPGbsJIH2ZnLYomGIVrk.jpg", "http://ia.media-imdb.com/images/M/MV5BMTIyMDUwMDc4OF5BMl5BanBnXkFtZTYwNTY2Nzk5._V1_.jpg", "http://ia.media-imdb.com/images/M/MV5BMTIyMDUwMDc4OF5BMl5BanBnXkFtZTYwNTY2Nzk5._V1_SX300.jpg"],
            "extra_thumbs" : ["https://image.tmdb.org/t/p/original/xvNOnVYmtkevs1kby3X1rQXb2iU.jpg", "https://image.tmdb.org/t/p/original/911LypVOfeIYCVmpFWJW0NpjIXG.jpg", "https://image.tmdb.org/t/p/original/7Zc0ubgNke1pGKyQjews7yMD0Jb.jpg", "https://image.tmdb.org/t/p/original/yIg8L8J7UFfKGCtoWDH7QTevs6C.jpg"],
            "poster_original" : ["https://image.tmdb.org/t/p/original/3CrNPUulPGbsJIH2ZnLYomGIVrk.jpg"],
            "landscape" : ["http://assets.fanart.tv/fanart/movies/584/moviethumb/2-fast-2-furious-51ab110bb72f4.jpg"],
            "actors" : {
                "Mark Boone Junior" : "https://image.tmdb.org/t/p/w185/nNVvaTLa8A5uwLXQvCsKoX4npwo.jpg",
                "Amaury Nolasco" : "https://image.tmdb.org/t/p/w185/tFH2iQmCIARuPJwjqcw7STwLeEr.jpg",
                "Edward Finlay" : "https://image.tmdb.org/t/p/w185/cHgCOdSLSvjYiKwhycwCH5X4Viu.jpg",
                "Cole Hauser" : "https://image.tmdb.org/t/p/w185/2izzcOrZpt22ObHjSNq3qRhrGHu.jpg",
                "Ludacris" : "https://image.tmdb.org/t/p/w185/xLfq9wFO6dtYuUK96hbJ8I8EKFw.jpg",
                "Jin Auyeung" : "https://image.tmdb.org/t/p/w185/kQIGxSJSAcc3zK37zRCGuhxdNmL.jpg",
                "James Remar" : "https://image.tmdb.org/t/p/w185/AeSuQlBclvWqztlpkf9nIhvvhsc.jpg",
                "Devon Aoki" : "https://image.tmdb.org/t/p/w185/mytZpvb5lykocoOCDelFy8Eqezm.jpg",
                "Eric Etebari" : "https://image.tmdb.org/t/p/w185/bNjZk4EPaItye1szWPkse08TSJZ.jpg",
                "Paul Walker" : "https://image.tmdb.org/t/p/w185/iqvYezRoEY5k8wnlfHriHQfl5dX.jpg",
                "Mo Gallini" : "https://image.tmdb.org/t/p/w185/fkc5N30a7ZvZkCJEnnqh24uQwDN.jpg",
                "Eva Mendes" : "https://image.tmdb.org/t/p/w185/6JoVMpn0CcZwb6JK7XW2E6ntU52.jpg",
                "Tyrese Gibson" : "https://image.tmdb.org/t/p/w185/8AaJIsIb7yJcfwcgbD7qsT6ameq.jpg",
                "Thom Barry" : "https://image.tmdb.org/t/p/w185/AhUa78UoZ5oGk5kWiatnDaPxSnO.jpg",
                "Michael Ealy" : "https://image.tmdb.org/t/p/w185/oqzMgyxyZUjE8kTpjHhQ6YlMPov.jpg"
            },
            "backdrop_original" : ["http://assets.fanart.tv/fanart/movies/584/moviebackground/2-fast-2-furious-50472dee97d56.jpg", "https://image.tmdb.org/t/p/original/8RkLHFm2PK4PO5W0WZA5LMXLZBz.jpg"],
            "clear_art" : ["http://assets.fanart.tv/fanart/movies/584/movieart/2-fast-2-furious-505432e6599e6.png"],
            "logo" : ["http://assets.fanart.tv/fanart/movies/584/movielogo/2-fast-2-furious-4fb3fa6854a36.png"],
            "banner" : ["http://assets.fanart.tv/fanart/movies/584/moviebanner/2-fast-2-furious-51ab1119963b1.jpg"],
            "backdrop" : ["https://image.tmdb.org/t/p/w1280/8RkLHFm2PK4PO5W0WZA5LMXLZBz.jpg"],
            "extra_fanart" : ["http://assets.fanart.tv/fanart/movies/584/moviebackground/2-fast-2-furious-50472e91d86ce.jpg", "http://assets.fanart.tv/fanart/movies/584/moviebackground/2-fast-2-furious-504ea84f0d19d.jpg", "http://assets.fanart.tv/fanart/movies/584/moviebackground/2-fast-2-furious-504ea84f0db42.jpg", "http://assets.fanart.tv/fanart/movies/584/moviebackground/2-fast-2-furious-5057078be2e3f.jpg"]
        },
        "actors" : ["Paul Walker", "Tyrese Gibson", "Eva Mendes", "Cole Hauser"],
        "writers" : ["Gary Scott Thompson (characters)", "Michael Brandt (story)", "Derek Haas (story)", "Gary Scott Thompson (story)", "Michael Brandt (screenplay)", "Derek Haas (screenplay)"],
        "runtime" : 107,
        "type" : "movie",
        "released" : "2003-06-02"
    },
    "_t" : "media",
    "releases" : [{
            "status" : "done",
            "files" : {
                "movie" : ["/volume1/video/2 Fast 2 Furious (2003).mkv"]
            },
            "_id" : "fbb7d7f6fb71458886edd115d58c3b96",
            "media_id" : "e9376ee00c85449ba776be784da40552",
            "_rev" : "000254f4",
            "_t" : "release",
            "is_3d" : false,
            "last_edit" : 1404427546,
            "identifier" : "tt0322259.AAC.dvdrip",
            "quality" : "dvdrip"
        }
    ],
    "title" : "2 Fast 2 Furious",
    "_rev" : "00045be8",
    "profile_id" : null,
    "_id" : "e9376ee00c85449ba776be784da40552",
    "category_id" : null,
    "type" : "movie",
    "files" : {
        "image_poster" : ["/usr/local/couchpotato/var/cache/47c2f59d078bc8179ee52dd882a352ca.jpg"]
    },
    "identifiers" : {
        "imdb" : "tt0322259"
    }
}

到目前为止,我所做的是:

String json = download(url);

    try {
        JSONObject result = new JSONObject(json);
        JSONArray results = result.getJSONArray("movies");

        for(int i = 0; i < results.length(); i++)
        {
             JSONObject movieObject = results.getJSONObject(i);
             CouchPotatoMovie movie = new CouchPotatoMovie(movieObject);
             movies.add(movie);
        }

因此图像应该在CouchPotatoMovie对象中。 但我不知道如何让他们离开那里。

1 个答案:

答案 0 :(得分:0)

从JSON字符串创建 JSONObject

JSONObject jsonObject = new JSONObject(jsonString);

并使用getJSONObject()方法:

JSONObject imagesJsonObject = jsonObject.getJSONObject("info").getJSONObject("images");

然后,您可以使用keys()获取密钥,并使用getJSONArray()getString()方法获取图片网址。


示例:

poster获取网址:

JSONArray posterJsonArray = imagesJsonObject.getJSONArray("poster");
List<String> urls = new ArrayList<String>();
for (int i = 0; i < posterJsonArray.length(); i++) {
    String url = posterJsonArray.getString(i);
    urls.add(url);
}