如何在某些条件下跳过JSON数据

时间:2016-01-03 11:19:22

标签: android json

我坚持了好几个星期。如果online的值为0,我试着从json跳过一些数据。 这是我的Json:

[{
    "id": "2",
    "name": "Carol",
    "online": "1"

}, {
    "id": "3",
    "name": "Tom",
    "online": "0"


}, {
    "id": "4",
    "name": "Joy",
    "online": "1"

}]

如果online的值为0,那么我想在我的列表视图中解析它:

[{
    "id": "2",
    "name": "Carol",
    "online": "1"

}, {
    "id": "4",
    "name": "Joy",
    "online": "1"

}]

这意味着我们跳过了online值为0的json块。 这有可能吗?如果有的话,是否有任何帮助来实现这一目标?

我如何解析JSON并将其添加到我的arraylist:

JsonArrayRequest jsonObjReq = new JsonArrayRequest(url,
 new Response.Listener<JSONArray>() {
  @Override
  public void onResponse(JSONArray response) {

  Log.d("Volley response:", response.toString());
  //listView.setVisibility(View.VISIBLE);
 progbar.setVisibility(View.GONE);

    // Parsing json
 for (int i = 0; i < response.length(); i++) {

  try {

 JSONObject obj = response.getJSONObject(i);

  Attendants model = new Attendants();

  model.setAttname(obj.getString("name"));

  model.setId(obj.getString("id"));
   //check for online value

if(obj.getString("online").equals("1")){

model.setOnline(obj.getString("online"));

 }else{
 //skip the block of JSON HOW??

 }
 modelList.add(model);

  } catch (JSONException e) {
 e.printStackTrace();
}

 }

1 个答案:

答案 0 :(得分:3)

if(obj.getString("online").equals("1")){

model.setOnline(obj.getString("online"));
modelList.add(model);

}
//skip the else block