用 Java 解码 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1927885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:31:00  来源:igfitidea点击:

Decode JSON data in Java

javajson

提问by Amit

I'm used to PHP, and decoding json data is just a line of code. What would be the easiest way to do this in java?

习惯了PHP,解码json数据只是一行代码。在 Java 中执行此操作的最简单方法是什么?

回答by hans

have a look at http://code.google.com/p/json-simple/maybe it helps ;-)

看看 http://code.google.com/p/json-simple/也许它有帮助;-)

回答by moonshadow

Pick one of the libraries from the Java section at the bottom of the json.orgpage.

json.org页面底部的 Java 部分选择一个库。

回答by Eli Acherkan

I like Flexjson. It's lightweight and easy to use.

我喜欢Flexjson。它重量轻且易于使用。

But I admit that I haven't bothered to compare all the alternatives :-)

但我承认我没有费心比较所有的选择:-)

回答by Frank Roth

I love Gson, it's very simple and easy to use. If you are interessted in more, here is a tutorial (german): http://blog.mynotiz.de/programmieren/java-json-decode-tutorial-2074/

我喜欢 Gson,它非常简单易用。如果您对更多感兴趣,这里有一个教程(德语):http: //blog.mynotiz.de/programmieren/java-json-decode-tutorial-2074/

回答by Sai Sunder

Decoding json in java is not too hard. Google's gson api handles json data very well. A tutorial on decoding json data using gson is there in my blog http://preciselyconcise.com/apis_and_installations/json_to_java.php

在java中解码json并不太难。Google 的 gson api 很好地处理了 json 数据。我的博客http://preciselyconcise.com/apis_and_installations/json_to_java.php 中有使用 gson 解码 json 数据的教程

回答by fabien

There are many JSON libraries available in Java.

Java 中有许多可用的 JSON 库。

The most notorious ones are: Hymanson, GSON, Genson, FastJsonand org.json.

最臭名昭著的是:HymansonGSONGensonFastJsonorg.json

There are typically three things one should look at for choosing any library:

选择任何图书馆通常应该考虑三件事:

  1. Performance
  2. Ease of use (code is simple to write and legible) - that goes with features.
  3. For mobile apps: dependency/jar size
  1. 表现
  2. 易于使用(代码易于编写且清晰易读)- 与功能相匹配。
  3. 对于移动应用程序:依赖项/jar 大小

Specifically for JSON libraries (and any serialization/deserialization libs), databinding is also usually of interest as it removes the need of writing boiler-plate code to pack/unpack the data.

特别是对于 JSON 库(以及任何序列化/反序列化库),数据绑定通常也很有趣,因为它不需要编写样板代码来打包/解包数据。

For 1, see this benchmark: https://github.com/fabienrenaud/java-json-benchmarkI did using JMHwhich compares (Hymanson, gson, genson, fastjson, org.json, jsonp) performance of serializers and deserializers using stream and databind APIs. For 2, you can find numerous examples on the Internet. The benchmark above can also be used as a source of examples...

对于 1,请参阅此基准测试:https: //github.com/fabienrenaud/java-json-benchmark我使用JMH比较了(Hymanson、gson、genson、fastjson、org.json、jsonp)使用流的序列化器和反序列化器的性能和数据绑定 API。对于 2,您可以在 Internet 上找到许多示例。上面的基准也可以用作示例的来源......

Quick takeaway of the benchmark: Hymansonperforms 5 to 6 times better than org.json and more than twice better than GSON.

快速了解基准:Hymanson 的性能比 org.json 好 5 到 6 倍,比 GSON 好两倍多。

Let me know if you have any questions.

如果您有任何问题,请告诉我。