Is it possible to convert a JavaScript JSON string into a Java object in a Java web application? I converted an ArrayList to a JSON string and sent it to a JSP page. Now, on the JSP page, I want to iterate through the JSON string. Is there a way to do this without converting it back to a Java object?
For example, how can I use the name from the JSON string to populate an input text box field?
[{"name":"john"}, {"lastname":"nice"}];
And then set the value in the input text field:
$("#textbox").val("namehere");
Is this achievable?
Here is my current process:
I have a button that triggers an AJAX call, and in the servlet, I convert an ArrayList to JSON.
ArrayList<UserProfile> users = new ArrayList<UserProfile>();
The UserProfile class consists of name, last name, and email fields.
users.add(new UserProfile("john", "garcia", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="117b7e797f517062753f727e7c">[email protected]</a>"));
users.add(new UserProfile("cena", "brock", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c1d1ccc0c8c0c6cdcce3c2d0c78dc0ccce">[email protected]</a>"));
Then, I return this data to the JSP page.
out.println(gson.toJson(users));
When receiving the data in the AJAX success function:
success: function(data) {
// I want to access every element of the ArrayList parsed to a JSON string
}