JSONArray中的唯一JSONObjects

时间:2016-11-07 12:52:13

标签: java android json

我有一个JSONArray,里面包含一组JSONObjects。从

获取唯一JSONObjects的最佳算法是什么
"lessaon_plan_data": [
      {
        "lessonplan_marks": 100,
        "lessonplan_name": "wdwd",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 50,
        "lessonplan_name": "ewewd",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 8,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 20,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 4,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 8,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 20,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 4,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      },
      {
        "lessonplan_marks": 8,
        "lessonplan_name": "qwefqwef",
        "lessonplan_subject": "Maths"
      }
    ]

我试过的是:

private JSONArray removeDuplicate(JSONArray rubricReportArray) {
    Log.e("MethodEntered", "success");
    JSONArray tempArray = new JSONArray();
    try {
        JSONObject tempStudentObj = null;
        for (int i = 0; i < rubricReportArray.length(); i++) {
            JSONObject studentObj = rubricReportArray.getJSONObject(i);


            tempStudentObj = new JSONObject();
            tempStudentObj.put("student_name", studentObj.getString("student_name"));
            tempStudentObj.put("lessonplan_name", studentObj.getString("lessonplan_name"));
            tempStudentObj.put("student_id", studentObj.getString("student_id"));
            tempStudentObj.put("lessonplan_subject", studentObj.getString("lessonplan_subject"));
            tempStudentObj.put("student_marks", studentObj.getString("student_marks"));
            tempStudentObj.put("lessonplan_class", studentObj.getString("lessonplan_class"));


            JSONArray duplicateArray = studentObj.getJSONArray("lessaon_plan_data");
            JSONArray uniqueArray = new JSONArray();
            Map<String,String> uniqueMap = new HashMap<>();

            for (int j = 0; j < duplicateArray.length(); j++) {
                boolean flag = false;
                String lessonMarks = duplicateArray.getJSONObject(j).getString("lessonplan_marks");
                String lessonName = duplicateArray.getJSONObject(j).getString("lessonplan_name");
                String lessonSubject = duplicateArray.getJSONObject(j).getString("lessonplan_subject");
                for (int k = j + 1; k < duplicateArray.length() - 1; k++) {
                    String currentLessonMarks = duplicateArray.getJSONObject(k).getString("lessonplan_marks");
                    String currentLessonName = duplicateArray.getJSONObject(k).getString("lessonplan_name");
                    String currentLessonSubject = duplicateArray.getJSONObject(k).getString("lessonplan_subject");
                    if (!lessonSubject.equalsIgnoreCase(currentLessonSubject)) {
                        uniqueArray.put(duplicateArray.getJSONObject(j));
                        break;
                    } else if (!lessonName.equalsIgnoreCase(currentLessonName)) {
                        flag = false;
                        uniqueArray.put(duplicateArray.getJSONObject(j));
                        break;
                    } else {
                        if (!lessonMarks.equalsIgnoreCase(currentLessonMarks)) {
                            flag = true;
                        }
                    }
                }
                if (flag) {
                    uniqueArray.put(duplicateArray.getJSONObject(j));
                }
                //Log.e("Unique JSON",set.toString());
            }
            tempStudentObj.put("lessaon_plan_data", uniqueArray);
            Log.e("TempStudent", tempStudentObj.toString());
            tempArray.put(tempStudentObj);

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

    return tempArray; //assign temp to original

}

我正在获得具有独特主题和课程的独特对象。但是当有相同的课程和相同的科目但分数不同时,问题就出现了。

如何从中获取唯一的JSONObject并将其存储在新的或替换为相同的JSONArray中? 我已经尝试了stackoverflow中的大多数解决方案。 但在我的情况下没有任何作用。 请帮忙。 提前致谢

2 个答案:

答案 0 :(得分:1)

目前还不清楚你做了什么或者如何存储对象。

但如果你还没有,那么我的建议是使用一个只收集唯一对象而不是数组的Java集合。

所以HashSetTreeSet或类似的东西。然后你可以;

  1. 在插入之前,请在插入之前检查对象是否存在,然后根据需要进行处理。
  2. 使用新对象插入和替换现有对象
  3. 请记住,对象的比较需要您在班级中创建自己的equals()比较(当然,您还需要覆盖hashcode()

    另请参阅:Prevent duplicate entries in arraylist

    <强> 更新

    我用Gson解码我的json,以下只给了我3个独特的条目;

    Gson gson = new Gson();
    Type listType = new TypeToken<HashSet<LessonPlan>>(){}.getType();
    Set<LessonPlan> lpList = new Gson().fromJson(json, listType);
    

    课程计划课

    public class LessonPlan {
        private int lessonplan_marks;
        private String lessonplan_name;
        private String lessonplan_subject;
    
        public int getLessonplan_marks() {
            return lessonplan_marks;
        }
    
        public void setLessonplan_marks(int lessonplan_marks) {
            this.lessonplan_marks = lessonplan_marks;
        }
    
        public String getLessonplan_name() {
            return lessonplan_name;
        }
    
        public void setLessonplan_name(String lessonplan_name) {
            this.lessonplan_name = lessonplan_name;
        }
    
        public String getLessonplan_subject() {
            return lessonplan_subject;
        }
    
        public void setLessonplan_subject(String lessonplan_subject) {
            this.lessonplan_subject = lessonplan_subject;
        }
    
        public LessonPlan() {
    
    
        }
    
        public String toString()
        {
            return "Name: " + this.getLessonplan_name() + " subject: " + this.getLessonplan_subject() + " marks: " + this.getLessonplan_marks();
        }
    
    
        public boolean equals(Object obj)
        {
            if (obj instanceof LessonPlan)
            {
                LessonPlan other = (LessonPlan)obj;
                if (other.getLessonplan_name().equals(this.getLessonplan_name()) && other.getLessonplan_subject().equals(this.getLessonplan_subject()))
                    return true;
                else
                    return false;
            }
            else
                return false;
        }
    
        public int hashCode()
        {
            return this.getLessonplan_name().hashCode() + this.getLessonplan_subject().hashCode();
        }
    

    当然这样做我无法控制保留哪个对象。

答案 1 :(得分:0)

private JSONArray removeDuplicate(JSONArray rubricReportArray) throws Exception{
        Log.e("MethodEntered", "success");
        JSONArray tempArray = new JSONArray();
        try {
            JSONObject tempStudentObj = null;
            for (int i = 0; i < rubricReportArray.length(); i++) {
                JSONObject studentObj = rubricReportArray.getJSONObject(i);


                tempStudentObj = new JSONObject();
                tempStudentObj.put("student_name", studentObj.getString("student_name"));
                tempStudentObj.put("lessonplan_name", studentObj.getString("lessonplan_name"));
                tempStudentObj.put("student_id", studentObj.getString("student_id"));
                tempStudentObj.put("lessonplan_subject", studentObj.getString("lessonplan_subject"));
                tempStudentObj.put("student_marks", studentObj.getString("student_marks"));
                tempStudentObj.put("lessonplan_class", studentObj.getString("lessonplan_class"));


                JSONArray duplicateArray = studentObj.getJSONArray("lessaon_plan_data");
                JSONArray uniqueArray = new JSONArray();
                int k;
                for (int j = 0; j < duplicateArray.length(); j++) {
                    boolean flag = false;
                    String lessonMarks = duplicateArray.getJSONObject(j).getString("lessonplan_marks");
                    String lessonName = duplicateArray.getJSONObject(j).getString("lessonplan_name");
                    String lessonSubject = duplicateArray.getJSONObject(j).getString("lessonplan_subject");
                    for (k = j + 1; k < duplicateArray.length() - 1; k++) {

                        String currentLessonMarks = duplicateArray.getJSONObject(k).getString("lessonplan_marks");
                        String currentLessonName = duplicateArray.getJSONObject(k).getString("lessonplan_name");
                        String currentLessonSubject = duplicateArray.getJSONObject(k).getString("lessonplan_subject");

                        if (lessonMarks.equalsIgnoreCase(currentLessonMarks) && (lessonSubject.equalsIgnoreCase(currentLessonSubject) &&
                                lessonName.equalsIgnoreCase(currentLessonName) 
                               ){
                            break;
                        }


                    }
                    if (k == duplicateArray.length() - 1){
                        uniqueArray.put(duplicateArray.getJSONObject(j));
                    }
                }
                tempStudentObj.put("lessaon_plan_data", uniqueArray);
                Log.e("TempStudent", tempStudentObj.toString());
                tempArray.put(tempStudentObj);

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

        return tempArray; //assign temp to original

    }

参考: - 用于从数组中查找唯一元素的基本算法 http://www.techcrashcourse.com/2015/08/c-program-print-unique-elements-unsorted-array.html