简体   繁体   中英

JSON, JS to Java and Vice Versa, and other languages

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?

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 .

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.

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)

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: Converting JSON to Java

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_JSON_%28_JavaScript_Object_Notation_%29

To serialize javascript objects for transmission to a server, I've used https://github.com/douglascrockford/JSON-js/blob/master/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM