views:

305

answers:

5

I want to use JSON to represent JS objects send them to a Java program, and vice versa. I would also like to do this for other languages. C# to Java maybe?

What do I use to do this? (Not sure if it is refered to as serialization or data binding)

Edit: Once a Java object is represented in JSON, does this mean that JavaScript can parse it and convert it to the corresponding JavaScript objects?

A: 

To go from JSON to objects in Java, I've heard that json-simple works well. It maps the JSON to a Java Map, which can contain String, Numbers, Lists and other Maps. This is a little simpler than some other libraries, which map directly to Java objects that you need to create to represent the JSON.

For an exhaustive list of JSON libraries in most major languages including both Java and C#, check out json.org.

Kaleb Brasee
A: 

You could use Google Web Toolkit to share objects between javascript and Java. With GWT you write all your code in Java and then the GWT compiler will handle the serialization of the RPC calls from javascript to Java and vice vera.

Brad
GWT is mega-overkill if all the OP needs is simple JSON serialization and deserialization.
Kaleb Brasee
Not necessarily. GWT abstracts away all the serialization and deserialization. I would actually argue the exact opposite. GWT seems to be a very very simple solution for passing objects between javascript and java.
Brad
A: 

If you mean over some sort connection (network, local pipe, etc), it would be called data serialization. You'd use a library to encode your objects. json.org has a list of libraries that can do what you want.

If you're writing a Java server with a JS front end, there's always GWT, too (I've never used, but heard great things about it)

Richard Levasseur
A: 

I would recommend using Gson for this. It has the advantage that it supports generics and nested beans very well and it is also fast enough. I've posted a Gson#fromJson() example which converts a fairly complex JSON string to Java here: http://stackoverflow.com/questions/1688099/converting-json-to-java/1688182#1688182

The Gson#toJson() to convert any valid Javabean-like object to JSON is basically a piece of cake:

String json = new Gson().toJson(object);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

Edit: Once a Java object is represented in JSON, does this mean that JavaScript can parse it and convert it to the corresponding JavaScript objects?

Sure you can access them like a JS object. If you're new to using JSON in JS as well, then I can recomment this kickoff tutorial: http://www.hunlock.com/blogs/Mastering%5FJSON%5F%28%5FJavaScript%5FObject%5FNotation%5F%29

BalusC
A: 

To serialize javascript objects for transmission to a server, I've used http://www.JSON.org/json2.js. Very nice and easy.

To get JSON data in and out of java, I found this library pretty straightforward: http://json-lib.sourceforge.net/

Creating javascript objects from JSON is a non-issue. JSON is valid javascript. You can simply run eval on it, or use a javascript library, which may offer some built-in security, which eval doesn't have.

morgancodes