从jsonarray android中删除空的json对象

时间:2019-05-14 07:39:48

标签: android json

我无法从数组android中删除空的JSON对象

"value":
    [
      {},
      {},
      {},
      {},
      {},
      {"value":"hshdhd","id":"78232102"}
    ]

1 个答案:

答案 0 :(得分:0)

您只需检查对象的长度即可删除空的Json对象。

请参见下面的示例

try {

    // This is your json strinh
    String json = "{\"value\":[{},{},{},{},{},{\"value\":\"hshdhd\",\"id\":\"78232102\"}]}";

    JSONObject jObj = new JSONObject(json);

    JSONArray jArr = new JSONArray(jObj.getString("value"));

    Log.w("Sample", "Json Array 1 >> " + jArr);

    JSONArray jArr2 = new JSONArray();

    for(int i = 0 ; i < jArr.length() ; i++) {

        JSONObject jObjIn = new JSONObject(jArr.getString(i));

        if(jObjIn.length() > 0)
            jArr2.put(jObjIn);
    }

    Log.w("Sample", "Json Array 2 >> " + jArr2);

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

输出:

enter image description here