【问题标题】:How to get rid of type warning when using JSONArray and JSONObject使用 JSONArray 和 JSONObject 时如何摆脱类型警告
【发布时间】:2017-09-17 20:31:15
【问题描述】:

我想通过使用 org.json.simple.JSONArray 和 org.json.simple.JSONObject 逐个创建 JSON 字符串。这是代码。

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);

第 2 行和第 3 行有 Eclipse 警告:

  • 类型安全:方法add(Object) 属于原始类型ArrayList。对泛型类型的引用应该被参数化。

第 5 行和第 6 行的警告:

  • 类型安全:方法put(object, object)属于原始类型HashMap。应该参数化对泛型类型 HashMap 的引用。

如何消除这些警告?

附言

这里解决的问题与How to correctly use HashMap? 不同,因为无法创建 JSONArrayJSONObject。 JSONArray 和 JSONObject 不可参数化。

【问题讨论】:

标签: java arrays json


【解决方案1】:

似乎无法通过其设计参数化 org.json.simple.JSONArray 和 org.json.simple.JSONObject。但是,您可以通过注释来抑制这些警告:

@SuppressWarnings("unchecked")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    相关资源
    最近更新 更多