如何在android中的jsonarray中解析具有相同名称的jsonobjects?

时间:2017-03-23 10:37:12

标签: android arrays json

{
    "terms": [{
        "offset": 0,
        "value": "Nerkundram"
    }, {
        "offset": 12,
        "value": "Chennai"
    }, {
        "offset": 21,
        "value": "Tamil Nadu"
    }, {
        "offset": 33,
        "value": "India"
    }]
}

1 个答案:

答案 0 :(得分:2)

使用此:

try{
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("terms");
for(int i=0;i<jsonArray.length();i++){
     JSONObject object = jsonArray.getJSONObject(i);
     String offset = object.getString("offset");
     String value = object.getString("value");
     //rest of the strings..
    }
 }
 catch (JSONException e){
       e.printStackTrace();
}