gpt4 book ai didi

java - JSONArray 中的唯一 JSONObject

转载 作者:行者123 更新时间:2023-12-01 09:17:07 24 4
gpt4 key购买 nike

我有一个 JSONArray,其中包含一组 JSONObject。从

获取唯一 JSONObject 的最佳算法是什么
"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

}

我得到了独特的元素,其中有独特的主题和类(class)。但是当相同的类(class)和相同的科目但分数不同时,问题就来了。

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

最佳答案

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

但是如果您还没有这样做,那么我的建议是使用仅维护唯一对象而不是数组的 Java 集合。

所以是 HashSetTreeSet 或类似的东西。那么你可以;

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

类(class)计划类(class)

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

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

关于java - JSONArray 中的唯一 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com