0

I am developing a web-app using AJAX requests on the client-side and Servlets on the server-side.

My aim is to send objects of Javascript to server, then do some manipulations there and send it back to show here.

Let's say my js object is

var obj={hero:"Spiderman",name:"Peter Parker"};

My Approach

1.Convert obj to JSON string and send

var str= JSON.stringify(obj);
xmlhttp.open("POST",myurl,true);
xmlhttp.setRequestHeader("Content-Type","application/json",true);
xmlhttp.send("data="+str); 

2. Recieve string,convert this back to JSON, manipulate "name" to "Bruce Wayne" and send it back as string

3.Recieve and convert back to Json

var data= JSON.parse(xmlhttp.responseText);

I am struggling at second point.I am using org.json for it .I searched and read docs but could not find satisfied answer for converting string to json and vica-versa in JAVA in my context.

It would be really helpful one could provide simple working code or point to some links where I can study.

P.S :

I cannot use Jquery as I am using AngularJS. See Why?

I will always send valid JSON string.

I can use other JSON lib. if its good than org.json and satisfy my needs. Please provide its jar download link.

Community
  • 1
  • 1
CoderBoy
  • 33
  • 7
  • Try this [Jquery Ajax Posting json to webservice - Stackoverflow][1] I hope it may help you. [1]: http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – OO7 Jun 24 '14 at 12:44
  • 1
    Maybe [decoding json examples](https://code.google.com/p/json-simple/wiki/DecodingExamples) will help - – James Jun 24 '14 at 12:49

1 Answers1

1

Assuming you are able to pull out data in your server code

This is how you can do it using org.json:

JSONParser parser = new JSONParser();
JSONObject requestObj = (JSONObject) parser.parse(data);
String name = (string)requestObj.get("name");
name = "Bruce Wayne";

Code to create the response can look something like this:

JSONObject response = new JSONObject();
response.put("name",name);
return response.toJSONString();

This assumes your server method returns a String type

And in case if you are using Servlet you can use HttpServletResponse object res to create response like:
res.setContentType("application/json"); OutputStream os = res.getOutputStream(); os.write(response.toString().getBytes()); os.close();

Shams
  • 3,637
  • 5
  • 31
  • 49
Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
  • Why would doing `name = "Bruce Wayne"` change the value? I'd expect that to change the value of `name` to a new String, but leave `requestObj`'s properties unchanged. – Anthony Grist Jun 24 '14 at 12:43
  • It won't change the original JSON object. This would be used inside the new JSON object that needs to be returned as String. – Nikhil Talreja Jun 24 '14 at 12:44
  • Why create a *new* JSON object, though? Just change the property in the one you've just created and then send it back. – Anthony Grist Jun 24 '14 at 12:50
  • That is up to the OP. The code should hopefully give an idea on how to work on step 2. – Nikhil Talreja Jun 24 '14 at 12:59
  • Thnx.Looking exactly for it. If you can please add some links for tutorials and How to send it back as a string :) – CoderBoy Jun 24 '14 at 13:02
  • 1
    Not aware of any tutorials as such. This is something I have done at work. I am sure you can google for some very easily :) – Nikhil Talreja Jun 24 '14 at 13:12