Currently, I am in the process of developing a web application that utilizes AJAX requests on the client-side and Servlets on the server-side.
The main goal is to send JavaScript objects to the server, perform manipulations there, and then return them to display on the client-side.
Let's assume my JavaScript object looks like this:
var obj={hero:"Spiderman",name:"Peter Parker"};
This is how I plan to approach it:
1. Convert the object to a JSON string and send it:
var str= JSON.stringify(obj);
xmlhttp.open("POST",myurl,true);
xmlhttp.setRequestHeader("Content-Type","application/json",true);
xmlhttp.send("data="+str);
2. Receive the string, convert it back to JSON, change the "name" value to "Bruce Wayne," and send it back as a string.
3. Receive and convert back to JSON:
var data= JSON.parse(xmlhttp.responseText);
I am currently facing difficulties at the second point. I have tried using org.json for this purpose but have not found a satisfactory solution for converting strings to JSON and vice versa in Java within my specific context.
If possible, could someone provide some simple working code or direct me to resources where I can further study this issue?
Also, please note that I cannot use jQuery due to my utilization of AngularJS. You can read more about it here.
Rest assured, I will always be sending valid JSON strings.
If there is an alternative JSON library that you recommend over org.json and that meets my requirements, please provide a link to download its JAR file.