2016-11-07 58 views
-1

我有一個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中的解決方案。 但在我的情況下沒有任何工作。 請幫忙。 在此先感謝

+0

顯示你的代碼中發現的獨特元素的基本算法中,你做了什麼? –

+0

我更新了問題,請檢查。 –

+0

已更正您的代碼並作爲答案發布。請檢查 –

回答

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

+0

我試過了,但沒有成功。 'k'甚至不在範圍內。你已經檢查了for循環之外的條件。並且for循環中的「if」條件將始終中斷,因爲所有主題都是數學。 –

+0

編輯我的答案,檢查完整的代碼。即使主題相同,名稱和標記也不同。代碼將檢查所有三個值是唯一的。 –

+1

非常感謝。它工作如此真棒。帽子掉了。 –

1

它不是很清楚你做了什麼或如何存儲對象。

但是,如果你還沒有,那麼我的建議是使用Java集合,它只保存唯一的對象,而不是數組。

所以一個HashSet,TreeSet或類似的東西。然後你可以;

  1. 在插入之前,請在插入之前檢查該對象是否存在,然後根據需要進行處理。
  2. 插入並與新的對象

記住對象的比較將要求您在類中創建自己的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); 

的lessonplan類

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(); 
    } 

當然做這種方式我無法控制的對象,其中被保留。

+0

我已經更新了我試過的問題。我只是想刪除重複的JSONObjects它。 JSONObject中的所有屬性應該是唯一的 –

+0

我仍然認爲處理Java集合是一種更好的方法。從JSON轉換爲obecjst​​,查找並刪除重複項,然後轉換回JSON。 –

+0

仍然沒有成功。 –