This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes Passing the JSON Array to the servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing the JSON Array to the servlet" Watch "Passing the JSON Array to the servlet" New topic
Author

Passing the JSON Array to the servlet

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528

Hi Ranchers ,

Can you please help me how can i retrieve the JSONArray Object inside my servlet.


I am passing this arrayObj inside the send () using AJAX.


If you want something you never had do something which you had never done
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Convert jsonObj to String type using toString method and send it to the client with content-type as “text/xml” or “text/plain”.

HTH

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
After getting the Object in the form of toString() inside the servlet . How can i retrieve each element on that Object ?

I read about eval() but confused.

How can i retrieve each project of the Object??
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
I got it. Thanks .
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Sorry, I posted "Server to client" way, Here is "client to server" mode


• Create jsonObj JavaScript object
• Use “POST” HTTP method in the open method of the XMLHttpRequest object
• Pass jsonObj JavaScript object in the send method of XMLHttpRequest object


look for JSON.stringify();
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Sagar , thanks .

But you did not answer my question . My question is after sending the JSONArray on to the send() with AJAX .
How can the servlet extract each property or element of the Object??
strangely , What you have posted is my question actually .
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

We're confusing each other

RaviNada Kiran wrote:
How can the servlet extract each property or element of the Object??

If you have a JSON string at server side, like "{key:value}" and you want to extract value/object then,

Create an JSONObject using JSONObject(String) constructor and use JSONObject#get(key) method.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
"and use JSONObject#get(key) method"

This is what i was looking for .Thanks Again.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Welcome
 
wood burning stoves
 
subject: Passing the JSON Array to the servlet