I want to send json data in url as below .
editTest.jsp?details=374889331-{\"aNumber\":2}
How can I do this?
We can convert the Object to JSON Object using GSON, then parse the JSON object and convert it to query param string. The Object should only contain primitive objects like int, float, string, enum, etc. Otherwise, you need to add extra logic to handle those cases.
public String getQueryParamsFromObject(String baseUrl, Object obj) {
JsonElement json = new Gson().toJsonTree(obj);
// Assumption is that all the parameters will be json primitives and there will
// be no complex objects.
return baseUrl + json.getAsJsonObject().entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue().getAsString())
.reduce((e1, e2) -> e1 + "&" + e2)
.map(res -> "?" + res).orElse("");
}