如何解析JSONArray在安卓 [英] how to parse JSONArray in android

查看:137
本文介绍了如何解析JSONArray在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读此JSON线,但因为它与JSONArray启动即时通讯有点糊涂了

 abridged_cast:
            {
                名:杰夫·布里吉斯
                ID:162655890,
                人物:
                    杰克prescott
                ]
            },
            {
                名:查尔斯·高汀,
                ID:162662571,
                人物:
                    弗雷德·威尔逊
                ]
            },
            {
                名:杰西卡·兰格
                ID:162653068,
                人物:
                    Dwan
                ]
            },
            {
                名:约翰·伦道夫
                ID:162691889,
                人物:
                    上校罗斯
                ]
            },
            {
                名:雷内·奥柏戎诺瓦
                ID:162718328,
                人物:
                    巴格利
                ]
            }
        ]
 

我只需要使用名,并保存所有作为一个字符串。 (字符串值将是:杰夫·布里奇斯,查尔斯·高汀,杰西卡·兰格,约翰·伦道夫,雷内·奥柏戎诺瓦)。

这是我的code:

 尝试{
        // JSON是JSON code以上

        JSONObject的jsonResponse =新的JSONObject(JSON);
        JSONArray电影= jsonResponse.getJSONArray(人物);
        字符串哎= movies.toString();


    }赶上(JSONException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
 

解决方案

如果您在名称之后,为什么你的code片段看起来像一个试图让人物?

不管怎么说,这是没有任何其他列表 - 或数组式的操作不同:你只需要遍历数据集,并抓住你感兴趣的信息检索所有的名字应该看起来有点像这样:

 名单,其中,字符串> allNames =新的ArrayList<字符串>();

JSONArray铸= jsonResponse.getJSONArray(abridged_cast);
的for(int i = 0; I< cast.length();我++){
    JSONObject的演员= cast.getJSONObject(我);
    字符串名称= actor.getString(姓名);
    allNames.add(名称);
}
 

(输入直入浏览器,因此未测试)。

I want to read this JSON lines but because it start with JSONArray im a little confused

 "abridged_cast": [
            {
                "name": "Jeff Bridges",
                "id": "162655890",
                "characters": [
                    "Jack Prescott"
                ]
            },
            {
                "name": "Charles Grodin",
                "id": "162662571",
                "characters": [
                    "Fred Wilson"
                ]
            },
            {
                "name": "Jessica Lange",
                "id": "162653068",
                "characters": [
                    "Dwan"
                ]
            },
            {
                "name": "John Randolph",
                "id": "162691889",
                "characters": [
                    "Capt. Ross"
                ]
            },
            {
                "name": "Rene Auberjonois",
                "id": "162718328",
                "characters": [
                    "Bagley"
                ]
            }
        ],

i just need to use the "name" and save all as one String. (the string value will be : Jeff Bridges,Charles Grodin,Jessica Lange,John Randolph,Rene Auberjonois).

this is my code:

try {
        //JSON is the JSON code above

        JSONObject jsonResponse = new JSONObject(JSON);
        JSONArray movies = jsonResponse.getJSONArray("characters");
        String hey = movies.toString();


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

解决方案

If you're after the 'name', why does your code snippet look like an attempt to get the 'characters'?

Anyways, this is no different from any other list- or array-like operation: you just need to iterate over the dataset and grab the information you're interested in. Retrieving all the names should look somewhat like this:

List<String> allNames = new ArrayList<String>();

JSONArray cast = jsonResponse.getJSONArray("abridged_cast");
for (int i=0; i<cast.length(); i++) {
    JSONObject actor = cast.getJSONObject(i);
    String name = actor.getString("name");
    allNames.add(name);
}

(typed straight into the browser, so not tested).

这篇关于如何解析JSONArray在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆