使用java将JSONObject添加到jsonarray时避免重复

使用java将JSONObject添加到jsonarray时避免重复,java,json,Java,Json,我有一个对象列表,正在转换为JSONArray。我正在迭代JSONObject并生成一个JSONObject数组 现在,我希望避免重复对象以插入到JSONArray中 请在下面找到我的java代码 JSONArray responseArray1 = new JSONArray(); if (!itemList.isEmpty()) { jsonArray = new JSONArray(itemList); for (int i = 0; i < jsonArray.length

我有一个对象列表,正在转换为JSONArray。我正在迭代JSONObject并生成一个JSONObject数组

现在,我希望避免重复对象以插入到JSONArray中

请在下面找到我的java代码

JSONArray responseArray1 = new JSONArray();
if (!itemList.isEmpty())
{
  jsonArray = new JSONArray(itemList);
  for (int i = 0; i < jsonArray.length(); i++)
  {
    JSONObject jsonObj = jsonArray.getJSONObject(i);
    JSONObject responseObj = new JSONObject();
    String attr_label = jsonObj.optString("attr_label");
    if(StringUtils.equalsIgnoreCase(attr_label, "long_description")) {
        long_description = jsonObj.optString("value");
    }
    else if(StringUtils.equalsIgnoreCase(attr_label, "description")) {
        description = jsonObj.optString("value");
    }
    responseObj.put("id", jsonObj.opt("id")); // i will get duplicate id
    responseObj.put("code", jsonObj.opt("code")); // i will get duplicate code
    responseObj.put("long_description", long_description);
    responseObj.put("description", description);
    responseArray1.put(responseObj);

  }
}
我期望像下面的jsonArray一样:

[  
   {  
      "code":"xyaz",
      "attr_label":"long_description",
      "id":"12717",
      "value":"Command Module"
   },
   {  
      "code":"xyaz",
      "attr_label":"description",
      "id":"12717",
      "value":"Set Point Adjustment"
   },
]
[  
   {  
      "code":"xyaz",
      "id":"12717",
      "long_description":"Command Module"
      "description" : "Set Point Adjustment"
   }
]
更新:

我已尝试使用以下代码,以避免重复插入
id
&
code
字段。但它不能正常工作。它还插入了重复项

List<String> dummyList=new ArrayList<String>();
JSONArray responseArray2 = new JSONArray(itemList);
if (!itemList.isEmpty())
{
  jsonArray = new JSONArray(itemList);
  for (int i = 0; i < jsonArray.length(); i++)
  {
    JSONObject jsonObj = jsonArray.getJSONObject(i);
    JSONObject responseObj = new JSONObject();

    String itemCode = jsonObj.optString("code");
    String id = jsonObj.optString("id");
    if(!dummyList.contains(itemCode) && !dummyList.contains(id) ) {
    dummyList.add(String.valueOf(jsonObj.opt("id")));
    dummyList.add(String.valueOf(jsonObj.opt("code")));
    responseObj.put("id", jsonObj.opt("id"));
    responseObj.put("code", jsonObj.opt("code"));
    responseObj.put("long_description", long_description);
    responseObj.put("description", description);
    responseArray2.put(responseObj);
    }
  }
}
List dummyList=new ArrayList();
JSONArray responseArray2=新JSONArray(项目列表);
如果(!itemList.isEmpty())
{
jsonArray=新jsonArray(项目列表);
for(int i=0;i
制作一个临时数组列表,并在该arrayList中添加唯一的代码,检查它是否已经存在于arrayList中,然后不要再放置此代码

String code = jsonObj.opt("code");
if(!arrayList.contains(code))
{
    arrayList.add(code);
    responseObj.put("id", jsonObj.opt("id")); 
    responseObj.put("code", jsonObj.opt("code")); 
    responseObj.put("long_description", long_description);
    responseObj.put("description", description);
}

使用TreeSet并将Comparator添加到其构造函数中,在该构造函数中比较对象的重复数据。 例如:-

     Set<Sample> sampleSet=new TreeSet<>(new Sample());
Set sampleSet=new TreeSet(new Sample());
示例类如下所示:-

 class Sample implements Camparator<Sample>{
    private String name;
    private String id;
   //getter 
   //setter

    @Override
    public String compare(Sample o1,Sample o2){
        return o1.getName.compareTo(o2.getName);
    }
    }
类示例实现Camparator{
私有字符串名称;
私有字符串id;
//吸气剂
//塞特
@凌驾
公共字符串比较(样本o1、样本o2){
返回o1.getName.compareTo(o2.getName);
}
}

这将提供一组唯一的名称条目。

如果您不想使用set,则可以使用此方法。可观的!如果(!dummyList.contains(itemCode)| |!dummyList.contains(id))如何在
compare
方法中再添加一个属性,请替换为&&if。类似于
getName()
&
getId()
在campare方法中有您的逻辑如果您希望名称和id都是唯一的,那么创建您自己的逻辑并在唯一的id和名称上返回“0”,类似于:-if(o1.getName.compareTo(o2.getName)==0)返回0;else if(o1.getId().compareTo(o2.getId())==0)返回0;否则返回o1.getName().compareTo(o2.getName);根据javadoc override equals,这种方法是好的,而hashcode方法是坏的方法。