如何在android中解析JSONArray

dig*_*ges 19 java arrays android json

我想读这一JSON行,但因为它开头JSONArray我有点困惑

 "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"
                ]
            }
        ],
Run Code Online (Sandbox Code Playgroud)

我只需要使用"名称"并将所有内容保存为一个字符串.(字符串值将是:Jeff Bridges,Charles Grodin,Jessica Lange,John Randolph,Rene Auberjonois).

这是我的代码:

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();
    }
Run Code Online (Sandbox Code Playgroud)

MH.*_*MH. 60

如果您在"名称"之后,为什么您的代码段看起来像是试图获取"字符"?

无论如何,这与任何其他列表或类似数组的操作没有什么不同:您只需要迭代数据集并获取您感兴趣的信息.检索所有名称应该看起来像这样:

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);
}
Run Code Online (Sandbox Code Playgroud)

(直接输入浏览器,因此未经过测试).

  • @numerah:你有一个没有键的数组,所以只需将json直接解析为`JSONArray`. (5认同)

小智 7

getJSONArray(attrname)将从你给定的属性名称的对象中获取一个数组,正在发生的是

{"abridged_cast":["name": blah...]}
^ its trying to search for a value "characters"
Run Code Online (Sandbox Code Playgroud)

但你需要进入数组,然后搜索"字符"

试试这个

String json="{'abridged_cast':[{'name':'JeffBridges','id':'162655890','characters':['JackPrescott']},{'name':'CharlesGrodin','id':'162662571','characters':['FredWilson']},{'name':'JessicaLange','id':'162653068','characters':['Dwan']},{'name':'JohnRandolph','id':'162691889','characters':['Capt.Ross']},{'name':'ReneAuberjonois','id':'162718328','characters':['Bagley']}]}";

    JSONObject jsonResponse;
    try {
        ArrayList<String> temp = new ArrayList<String>();
        jsonResponse = new JSONObject(json);
        JSONArray movies = jsonResponse.getJSONArray("abridged_cast");
        for(int i=0;i<movies.length();i++){
            JSONObject movie = movies.getJSONObject(i);
            JSONArray characters = movie.getJSONArray("characters");
            for(int j=0;j<characters.length();j++){
                temp.add(characters.getString(j));
            }
        }
        Toast.makeText(this, "Json: "+temp, Toast.LENGTH_LONG).show();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

检查了它 :)