• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to build and pass JSON objects back to a server?

 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all-
In our app, we are using seam remoting to pass objects to an xhtml client from our ejbs. This works very well but the client to server interaction is much more restricted and we can't pass complex objects back via seam.
So I need to send a collection of objects back to my java server ejb, format them as an XML doc and save.

I'm thinking of building JSON objects, posting them as a string and having my server side code use xstream to format xml.
I think this provides a structured interface compared to writing my own solution (hashmaps, name-value pairs, what-have-you).

Is there client side code that will help me do this in javascript? (build JSON objects?)
Or should I simply format the array of objects by hand into JSON? This doesn't seem too tough actually but I wanted to leverage any solutions already out there.

thanks!
Max
 
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What browsers are you supporting? Modern browsers will possess JSON.stringify().
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear-
Thanks for the reply. We are supporting the usual suspects - IE 7+, FF.
I think I need to get up to speed on JSON. I was at a loss how to create the objects on the client side. I thought I had to physically code the syntax e.g. "[ :" etc.

But it seems I create the objects and use eval: e.g. this sample:

var arrayAsJSONText = '["Europe", "Asia", "Australia", "Antarctica",
"North America", "South America", "Africa"]';
var continents = eval( arrayAsJSONText );
alert(continents[0] + " is one of the " + continents.length + "
continents.");

or as you suggest:

alert("The JSON representation of the continents array is: " +
continents.toJSONString());

But I need objects with member variables e.g. a column object (one of our application objects) which contain various fields: width, alignment, etc. I want it to map to a pojo so I can pass it to the server and convert it to a pojo.

thanks

Max

 
Bear Bibeault
Sheriff
Posts: 67693
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, JSON supports that. You seem to be focused on arrays only. Visit json.org for a lot of info on JSON.
 
reply
    Bookmark Topic Watch Topic
  • New Topic