在 Java 中存储 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23956154/
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-14 09:24:10  来源:igfitidea点击:

Store JSON data in Java

javajsongsonjson-simple

提问by Francesco Menzani

I want to learn JSON data storage in Java using Eclipse, so I googled a lot.

我想使用 Eclipse 学习 Java 中的 JSON 数据存储,所以我搜索了很多。

I found JSON.simpleand GSON. Are those a good choice?

我找到了JSON.simpleGSON。这些是不错的选择吗?

I went to Properties >> Java Build Path >> Add External JARs. I added json-simple-1.1.1.jarand google-gson-2.2.4-release.zip. Is it right? Where is the JavaDoc located?

我去了Properties >> Java Build Path >> Add External JARs。我添加了json-simple-1.1.1.jargoogle-gson-2.2.4-release.zip。这样对吗?JavaDoc 位于何处?

Main problem concerns an example found here: JSON.simple example – Read and write JSON, which teaches about JSON.simple. I wrote the following code:

主要问题与此处找到的示例有关:JSON.simple example – Read and write JSON,它教授 JSON.simple 。我写了以下代码:

JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.add("Joiner");
array.add("System");
object.put("nicknames", array);
object.put("from", "Italy");
object.put("age", 15);
try {
    String path = "";
    FileWriter file = new FileWriter(path);
    String json = object.toJSONString();
    file.write(json);
    file.close();
} catch (IOException io) {
    // Fail
}

But I get the following warnings. What do these mean?

但我收到以下警告。这些是什么意思?

Type safety: The method add(Object) belongs to the raw type ArrayList. References to generic type ArrayList should be parameterized on 'JSONArray.add'

Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap should be parameterized on 'JSONObject.put'

类型安全:方法 add(Object) 属于原始类型 ArrayList。对泛型类型 ArrayList 的引用应该在“JSONArray.add”上参数化

类型安全: put(Object, Object) 方法属于原始类型 HashMap。对泛型类型 HashMap 的引用应该在 'JSONObject.put' 上参数化

On a side note, how can I prettify my JSON?

附带说明一下,我如何美化我的 JSON?

采纳答案by Francesco Menzani

I found JSON.simpleand GSON. Are those a good choice?

我找到了JSON.simpleGSON。这些是不错的选择吗?

At the moment, I like the former very much, but the latter has more features, is more widely used, and is always up-to-date.

目前,我非常喜欢前者,但后者功能更多,使用范围更广,而且总是与时俱进。

Where is the JavaDoc located?

JavaDoc 位于何处?

For JSON.simple, see this answeron the site. For GSON, follow this link.

对于 JSON.simple,请参阅网站上的此答案。对于 GSON,请点击此链接

I get some warnings. What do they mean?

我收到一些警告。他们的意思是什么?

Those classes are declared without type parameters for their superclass:

这些类的超类声明没有类型参数:

public class JSONObject extends HashMap {
    // ...
}

public class JSONArray extends ArrayList {
    // ...
}

When you call put()or add(), they are defined in the superclass, which is not parameterized properly.

当您调用put()or 时add(),它们是在超类中定义的,没有正确参数化。

What you can do is add @SuppressWarnings("unchecked")to the container.

您可以做的是添加@SuppressWarnings("unchecked")到容器中。

How can I prettify my JSON?

如何美化我的 JSON?

JSON.simple cannot do this, but GSON can:

JSON.simple 不能这样做,但 GSON 可以:

Gson gson = new GsonBuilder().setPrettyPrinting().create();