从JSON对象中提取图像

时间:2015-09-21 05:39:06

标签: android json

我正在从api中获取最受欢迎的电影,我正在尝试获取每部电影的海报图像。为此,我需要从json字符串中提取 poster_path 参数。

这是我从JSON字符串中提取信息的代码:

final String OWM_RESULTS = "results";
final String OWM_POSTERPATH = "poster_path";

String aux;

// movieJSONstr is the string with the full http request
JSONObject movieJson = new JSONObject(movieJSONstr);
JSONArray movieArray = movieJson.getJSONArray(OWM_RESULTS);

for(int i = 0; i < movieArray.length(); i++) {
    JSONObject movieObj = movieArray.getJSONObject(i);
    aux = movieObj.getString(OWM_POSTERPATH);
    Log.v("AUX: ", aux);
}

JSON字符串如下所示:

{
"page":1,
"results":[
  {
     "adult":false,
     "backdrop_path":"/tbhdm8UJAb4ViCTsulYFL3lxMCd.jpg",
     "genre_ids":[
        53,
        28,
        12
     ],
     "id":76341,
     "original_language":"en",
     "original_title":"Mad Max: Fury Road",
     "overview":"An apocalyptic story set in the furthest reaches of our planet, in a stark desert landscape where humanity is broken, and most everyone is crazed fighting for the necessities of life. Within this world exist two rebels on the run who just might be able to restore order. There's Max, a man of action and a man of few words, who seeks peace of mind following the loss of his wife and child in the aftermath of the chaos. And Furiosa, a woman of action and a woman who believes her path to survival may be achieved if she can make it across the desert back to her childhood homeland.",
     "release_date":"2015-05-15",
     "poster_path":"/kqjL17yufvn9OVLyXYpvtyrFfak.jpg",
     "popularity":46.603256,
     "title":"Mad Max: Fury Road",
     "video":false,
     "vote_average":7.6,
     "vote_count":2296
  }]
}

假设JSONObject确实有图像,我如何提取它们并将它们转换成我可以像Drawable实例一样操作的东西?

3 个答案:

答案 0 :(得分:0)

Car = new Mongo.Collection('car', {
    transform: function(entry) {
        entry.is_watched = function(userId) {
            var is_watched = false;
            if (entry.watchList) {
                for (var i in entry.watchList) {
                    if (entry.watchList[i].userId == userId) {
                        is_watched = true;
                        break;
                    }
                }
            }
            return is_watched;
        };
        return entry;
    }
});

答案 1 :(得分:0)

通常有两种通过JSON传递图像的方法。 首先,在我看来,最好的是将原始图像数据放入JSON对象。您可以使用base64对图像进行编码,并将其作为字符串放入对象中,然后解码base64并使用BitmapFactory类解码原始数据。

第二种更好的方法是将图像作为URL传递,看来您呈现的Json正在使用此方法:

"poster_path":"/kqjL17yufvn9OVLyXYpvtyrFfak.jpg"

这个问题最原始的解决方案是akhil batlawala的回答。对于真实世界的应用程序,您可能应该寻找像Picasso这样的图像库,其用法如下:

Picasso picasso = new Picasso();
picasso.load(url).fit().into(myImageView);

答案 2 :(得分:0)

尝试以下代码,

String response = "Your string";
         try {
            JSONObject jObject=new JSONObject(response);
            JSONArray jArray=jObject.getJSONArray("results");
            JSONObject obj=jArray.getJSONObject(0);
            String poster_path=obj.getString("poster_path");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

那是