-1

我已经浏览了一个关于在 Android 上解析 Json 数据的链接,但不太了解。我从我的服务器获取以下 JSON 字符串。

我需要解析它并将其呈现为我的 android 应用程序中的列表视图。任何指导或代码片段。?另外,由于我的 Jason 字符串有图像数据,列表视图是否能够正确显示它?

我的 JSON 字符串是:

{"results":{"result":[{"year":"2012","title":"The Amazing Spider-Man","details":"http:\/\/www.imdb.com\/title\/tt0948470","director":"Marc Webb","rating":"7.3","cover":"http:\/\/i.media-imdb.com\/images\/SF1f0a42ee1aa08d477a576fbbf7562eed\/realm\/feature.gif"},

{"year":"2014","title":"The Amazing Spider-Man 2","details":"http:\/\/www.imdb.com\/title\/tt0948470","director":"Marc Webb","rating":"7.3","cover":"http:\/\/ia.media-imdb.com\/images\/M\/MV5BMzk3MTE5MDU5NV5BMl5BanBnXkFtZTYwMjY3NTY3._V1._SX54_CR0,0,54,74_.jpg"},

{"year":"2002","title":"Spider-Man","details":"http:\/\/www.imdb.com\/title\/tt0948470","director":"Sam Raimi","rating":"6.3","cover":"http:\/\/ia.media-imdb.com\/images\/M\/MV5BODUwMDc5Mzc5M15BMl5BanBnXkFtZTcwNDgzOTY0MQ@@._V1._SX54_CR0,0,54,74_.jpg"},

{"year":"2007","title":"Spider-Man 3","details":"http:\/\/www.imdb.com\/title\/tt1872181","director":"Sam Raimi","rating":"7.5","cover":"http:\/\/ia.media-imdb.com\/images\/M\/MV5BMjE1ODcyODYxMl5BMl5BanBnXkFtZTcwNjA1NDE3MQ@@._V1._SX54_CR0,0,54,74_.jpg"},

{"year":"2004","title":"Spider-Man 2","details":"http:\/\/www.imdb.com\/title\/tt1872181","director":"Sam Raimi","rating":"6.8","cover":"http:\/\/i.media-imdb.com\/images\/SFa26455c07afc3c94f52e95de50d5d814\/realm\/tv_series.gif"}]}} 
4

2 回答 2

1

制作所有字段的 Arraylist 并编写下面的函数来解析上面的 json,它将解决您的问题。

ArrayList<String> year, title, details, director, rating, cover;
// For Parse Login Response From Server
public void mParseResponse() throws UnknownHostException { 

    year=new ArrayList<String>();
    title=new ArrayList<String>();
    details=new ArrayList<String>();
    director=new ArrayList<String>();
    rating=new ArrayList<String>();
    cover=new ArrayList<String>();

    try {
        JSONObject jObject = new JSONObject(EntityUtils.toString(entity));
        JSONObject jsonobjresults = jObject.getJSONObject("results");
        JSONArray jsonarrayresult = jsonobjresults.getJSONArray("result");
        for(int i=0;i<jsonarrayresult.length(); i++){
            JSONObject mJsonObj = jsonarrayresult.getJSONObject(i);
            year.add(mJsonObj.getString("year"));
            title.add(mJsonObj.getString("title"));
            details.add(mJsonObj.getString("details"));
            director.add(mJsonObj.getString("director"));
            rating.add(mJsonObj.getString("rating"));
            cover.add(mJsonObj.getString("cover"));
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
于 2012-11-30T09:22:31.343 回答
0

阅读本教程http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

您可以使用 Google 的 gson 库来解析 json。您需要创建 java 类来存储 json 值将该类引用传递给 gson 库。它有简单的方法。

检查这个文档

于 2012-11-30T09:26:17.933 回答