Android JSONArray去重

时间:2025-04-02 08:31:50
  • private static JSONArray delRepeatIndexid(JSONArray array) {
  • JSONArray arrayTemp = new JSONArray();
  • int num = 0;
  • try {
  • for (int i = 0; i < array.length(); i++) {
  • if (num == 0) {
  • (array.get(i));
  • } else {
  • int numJ = 0;
  • for (int j = 0; j < arrayTemp.length(); j++) {
  • JSONObject newJsonObjectI = (JSONObject) array.get(i);
  • JSONObject newJsonObjectJ = (JSONObject) arrayTemp.get(j);
  • String index_idI = newJsonObjectI.get("riskCode").toString();
  • String valueI = newJsonObjectI.get("riskName").toString();
  • String payIntvI = newJsonObjectI.get("payIntv").toString();
  • String yearsI = newJsonObjectI.get("years").toString();
  • String premI = newJsonObjectI.get("prem").toString();
  • String payYearsI = newJsonObjectI.get("payYears").toString();
  • String index_idJ = newJsonObjectJ.get("riskCode").toString();
  • if (index_idI.equals(index_idJ)) {
  • (j);
  • JSONObject newObject = new JSONObject();
  • ("riskCode", index_idI);
  • ("riskName", valueI);
  • ("payIntv", payIntvI);
  • ("years", yearsI);
  • ("prem", premI);
  • ("payYears", payYearsI);
  • (newObject);
  • break;
  • }
  • numJ++;
  • }
  • if (numJ - 1 == arrayTemp.length() - 1) {
  • (array.get(i));
  • }
  • }
  • num++;
  • }
  • } catch (JSONException e) {
  • ();
  • }
  • return arrayTemp;
  • }