Hi All,
JSONObject responseObj= {"pageinfo":[{"name":"page1", "path":"/content/proj/page1"} ,{"name":"page2","path":"/content/proj/page2"} , {"name":"page1", "path":"/content/proj/page1"} ]}
How to remove the duplicates?
I want responseObj as below
responseObj= {"pageinfo":[{"name":"page1", "path":"/content/proj/page1"} ,{"name":"page2","path":"/content/proj/page2"} ]}
Thanks in Advance
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Helen_DD
You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.
Added code snippet for each of the steps.
Please see below:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Example {
@SerializedName("pageinfo")
@Expose
private List<Pageinfo> pageinfo = null;
public List<Pageinfo> getPageinfo() {
return pageinfo;
}
public void setPageinfo(List<Pageinfo> pageinfo) {
this.pageinfo = pageinfo;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Pageinfo {
@SerializedName("name")
@Expose
private String name;
@SerializedName("path")
@Expose
private String path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Gson gson = new Gson(); // Create a GSON Object
Example exampleObj = gson.fromJson(initialResponse, Example.class); // Convert Json String to Gson Object
List<Pageinfo> pageinfoList = exampleObj.getPageinfo(); // Get the Page Info Details
TreeSet<Pageinfo> pageinfoTreeSet = pageinfoList.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Pageinfo::getName)))); // Remove the duplicate object using the 'name' field
exampleObj.setPageinfo(new ArrayList<>(pageinfoTreeSet)); // Set the Page Into back to List
String requiredResponse = gson.toJson(exampleObj); // Convert the list into Json String
Hope this helps!
Thanks!
Hi @Helen_DD
You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.
Added code snippet for each of the steps.
Please see below:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Example {
@SerializedName("pageinfo")
@Expose
private List<Pageinfo> pageinfo = null;
public List<Pageinfo> getPageinfo() {
return pageinfo;
}
public void setPageinfo(List<Pageinfo> pageinfo) {
this.pageinfo = pageinfo;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Pageinfo {
@SerializedName("name")
@Expose
private String name;
@SerializedName("path")
@Expose
private String path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Gson gson = new Gson(); // Create a GSON Object
Example exampleObj = gson.fromJson(initialResponse, Example.class); // Convert Json String to Gson Object
List<Pageinfo> pageinfoList = exampleObj.getPageinfo(); // Get the Page Info Details
TreeSet<Pageinfo> pageinfoTreeSet = pageinfoList.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Pageinfo::getName)))); // Remove the duplicate object using the 'name' field
exampleObj.setPageinfo(new ArrayList<>(pageinfoTreeSet)); // Set the Page Into back to List
String requiredResponse = gson.toJson(exampleObj); // Convert the list into Json String
Hope this helps!
Thanks!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Like
Replies