my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes JSON response to populate listbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JSON response to populate listbox" Watch "JSON response to populate listbox" New topic

JSON response to populate listbox

Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
Can any one have the code to use JSON in java.
[Aim:Populate a listbox from a server side response
:@Return a JSON Object from servlet,
==>created using JSON tag library and not hard coded json file].

IN particular I need to populate a dropdown box using JSON response from servlet.I am using JSF 1.1 and my JS Library is Jquery 1.2.6
Help me with a code.

I browsed through and found json implementations in both javascript and plain JSON ,but I need to do it from a servlet.Im using JSON Library too.

How can I do it with JSONObject,to contain all those options and its values.

Help me with a code.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961

With state-of-the-art JSF taglibraries, you shouldn't have to be worrying about whether the listbox is populated using JSON or not - the whole process is transparent, whether using the basic static listbox or one that's populated via AJAX. You define a listbox control using the JSF tag, define a list model in the backing bean, add some AJAX rules to the page definition if desired, and the data transfer takes care of itself.

On the other hand, if you're trying to adapt an existing JSON data source servlet, JSF doesn't directly support that. You'd either need to provide your own client-side JSON listbox code or make the JSF backing bean act as a proxy to the JSON datasource and use a regular (or AJAX-enhanced) JSF listbox on the client.

Customer surveys are for companies who didn't pay proper attention to begin with.
Dishpal Bhaluja
Ranch Hand

Joined: Dec 30, 2007
Posts: 96
It has nothing to do with JSF Actually,I am using json-lib in my bean class.
ie I can create objects of JSONArray,JSONobject and can serialise data and all defined in site.

I just want my servlet to return a JSON object containing all the options and its values.
I used something like

JSONObject json=new JSONObject();
problem is that i wont be able to access it correctly.I want a technique to resolve this.
I tried creating an objectArray and putting array to another JSON but that hadnt worked for me.

Hope my doubts are clear to you

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15961

OK. Your problem is that you asked in the wrong forum. We have an area for questions regarding HTML, JavaScript, AJAX and related things and that's where to find the experts in such matters. I'll ask Paul to move this thread over there.

As I mentioned before, the JSF selection listbox controls expect to be totally controlled by JSF including the data transfers of the list and its selection, so to use JSON, you'd need to use a non-JSF control or else do the JSON stuff inside the JSF backing bean instead of in the client's browser.
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15376
Moving to HTML/JS ... See the CLICK HERE link on top to goto the moved thread.

It is sorta covered in the JavaRanch Style Guide.
subject: JSON response to populate listbox
Similar Threads
Iterating a JSON in jQuery
JSON Object and Java Script DropDown
Populating List box with data from arrayList
AJAX Frameworks that integrate with Struts