我有一個JSONArray,它由一組JSONObjects組成。什麼是從JSONArray中的唯一JSON對象
"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"
}
]
獲得唯一一個JSONObjects最好的算法,我曾嘗試是這樣的:
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
}
我得到唯一對象那裏有獨特的主題和教訓。但是,如果有相同的課程和相同的主題,但分數不同,那麼問題就來了。
如何從中獲取唯一的JSONObjects並將其存儲在一個新的或替換在相同的JSONArray? 我已經嘗試了大多數在stackoverflow中的解決方案。 但在我的情況下沒有任何工作。 請幫忙。 在此先感謝
顯示你的代碼中發現的獨特元素的基本算法中,你做了什麼? –
我更新了問題,請檢查。 –
已更正您的代碼並作爲答案發布。請檢查 –