sagar suri sagar suri - 28 days ago 13
Java Question

Unique JSONObjects in JSONArray

I have a JSONArray which consist of set of JSONObjects in it. What is the best algorithm to get unique JSONObjects from

"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"
}
]


What I have tried is this:

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

}


I am getting unique objects where there is unique subject and lessons. But when there are same lessons and same subjects but the score differs then problem comes.

How to get unique JSONObjects from it and store it in a new or replace in the same JSONArray?
I have tried most of the solutions which is in stackoverflow.
But nothing worked in my condition.
Please help.
Thanks in advance

Answer
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

    }

Reference for basic algo behind finding unique elements from an array http://www.techcrashcourse.com/2015/08/c-program-print-unique-elements-unsorted-array.html

Comments