简体   繁体   English

JSON,JS到Java和Vice Versa等语言

[英]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. 我想使用JSON表示JS对象,将它们发送到Java程序,反之亦然。 I would also like to do this for other languages. 我也想针对其他语言进行此操作。 C# to Java maybe? 从C#到Java?

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? 编辑:用JSON表示Java对象后,这是否意味着JavaScript可以解析它并将其转换为相应的JavaScript对象?

To go from JSON to objects in Java, I've heard that json-simple works well. 为了从JSON转换为Java中的对象,我听说json-simple很好用。 It maps the JSON to a Java Map, which can contain String, Numbers, Lists and other Maps. 它将JSON映射到Java Map,该Java Map可以包含字符串,数字,列表和其他Map。 This is a little simpler than some other libraries, which map directly to Java objects that you need to create to represent the JSON. 这比其他一些库简单一些,其他一些库直接映射到您需要创建以表示JSON的Java对象。

For an exhaustive list of JSON libraries in most major languages including both Java and C#, check out json.org . 有关包括Java和C#在内的大多数主要语言的JSON库的详尽列表,请查看json.org

You could use Google Web Toolkit to share objects between javascript and Java. 您可以使用Google Web Toolkit在javascript和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. 使用GWT,您可以用Java编写所有代码,然后GWT编译器将处理从javascript到Java以及从vera到RPC调用的序列化。

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. json.org列出了可以满足您需求的库。

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) 如果您正在编写带有JS前端的Java服务器,那么也总会有GWT (我从没用过,但听说过很棒的东西)

I would recommend using Gson for this. 我建议为此使用Gson It has the advantage that it supports generics and nested beans very well and it is also fast enough. 它的优点是可以很好地支持泛型和嵌套bean,而且速度也足够快。 I've posted a Gson#fromJson() example which converts a fairly complex JSON string to Java here: Converting JSON to Java 我在Gson#fromJson()发布了一个Gson#fromJson()示例,该示例将相当复杂的JSON字符串转换为Java将JSON转换为Java

The Gson#toJson() to convert any valid Javabean-like object to JSON is basically a piece of cake: 将任何有效的类似于Javabean的对象转换为JSON的Gson#toJson()基本上是小菜一碟:

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? 编辑:用JSON表示Java对象后,这是否意味着JavaScript可以解析它并将其转换为相应的JavaScript对象?

Sure you can access them like a JS object. 确保您可以像JS对象一样访问它们。 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 如果您还不熟悉在JS中使用JSON,那么我可以推荐这个入门教程: 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 . 为了序列化javascript对象以传输到服务器,我使用了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/ 为了将JSON数据传入和传出Java,我发现该库非常简单: http : //json-lib.sourceforge.net/

Creating javascript objects from JSON is a non-issue. 从JSON创建javascript对象不是问题。 JSON is valid javascript. JSON是有效的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. 您可以简单地在其上运行eval,或使用javascript库,该库可能提供一些内置的安全性,而eval没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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