简体   繁体   English

使用JSONArray和JSONObject时如何摆脱类型警告

[英]How to get rid of type warning when using JSONArray and JSONObject

I want to create a JSON string piece by piece and by using org.json.simple.JSONArray and org.json.simple.JSONObject. 我想通过使用org.json.simple.JSONArray和org.json.simple.JSONObject来逐段创建JSON字符串。 Here's the code. 这是代码。

1. JSONObject config = new JSONObject();

2. JSONArray urls = new JSONArray();
3. urls.add("https://www.test1.com/v1");
4. urls.add("https://www.test1.com/v2");

5. config.put("name", "name-test1");
6. config.put("sipUrls", sipUrls);

There is Eclipse warning for line 2 and 3: 第2行和第3行有Eclipse警告:

  • type safety: the method add(Object) belongs to the raw type ArrayList. 类型安全:方法add(Object)属于原始类型ArrayList。 references to generic type should be parameterized. 对泛型类型的引用应参数化。

And warning for line 5 and 6: 并警告第5行和第6行:

  • type safety: the method put(object, object) belongs to the raw type HashMap. 类型安全:put(object,object)方法属于原始类型HashMap。 references to generic type HashMap should be parameterized. 泛型HashMap的引用应参数化。

How can I get rid of these warnings? 我如何摆脱这些警告?

PS PS

The problem addressed here is different from this How to correctly use HashMap? 这里解决的问题与此不同。 如何正确使用HashMap? because there's no way to create a JSONArray<String> or JSONObject<String> . 因为无法创建JSONArray <String>JSONObject <String> JSONArray and JSONObject are not parameterizable. JSONArray和JSONObject不可参数化。

It appears that there's no way to parameterize org.json.simple.JSONArray and org.json.simple.JSONObject by its design. 似乎无法通过其设计参数化org.json.simple.JSONArray和org.json.simple.JSONObject。 However, you can suppress these warnings by annotation: 但是,您可以通过注释禁止显示这些警告:

@SuppressWarnings("unchecked")

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

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