在json数组中的元素计数

时间:2016-07-28 06:00:29

标签: android json

这是我的JSON数组,

[{
    "textinput": [{
        "position": 0,
        "dependency": "no",
        "id": 0,
        "label": "single1",
        "Itype": "textinput"
    }, {
        "position": 1,
        "dependency": "no",
        "id": 1,
        "label": "single2",
        "Itype": "textinput"
    }, {
        "position": 2,
        "dependency": "no",
        "id": 2,
        "label": "single3",
        "Itype": "textinput"
    }, {
        "position": 3,
        "dependency": "no",
        "id": 3,
        "label": "single4",
        "Itype": "textinput"
    }, {
        "position": 4,
        "dependency": "no",
        "id": 4,
        "label": "single5",
        "Itype": "textinput"
    }, {
        "position": 5,
        "dependency": "no",
        "id": 5,
        "label": "single6",
        "Itype": "textinput"
    }]
}]

我想帮助计算上面数组中position的数量。

2 个答案:

答案 0 :(得分:1)

你错过了那里的那个:P

try {
     JSONArray jsonArray = new JSONArray("Your response");    
     int count = jsonArray.getJSONObject(0).getJSONArray("textinput").length();
 } catch (JSONException e) {
     e.printStackTrace();
 }

答案 1 :(得分:0)

String [] array = text.split(“position”); //发表你的回应json String

int count = array.length-1; //位置数