【问题标题】:unchecked call to add(E) as a member of the raw type ArrayList and HashMap未经检查地调用 add(E) 作为原始类型 ArrayList 和 HashMap 的成员
【发布时间】:2014-07-03 08:28:23
【问题描述】:

我有以下使用 org.json.simple 创建 JSON 对象的函数。

public JSONObject createJSONRequest() {
    // /* Create JSON Objects */
    JSONObject jsonObject = new JSONObject();
    Map<String, String> map = new HashMap<String, String>();

    map.put(ACCESS_TOKEN_KEY, mAccessToken);            
    map.put(SESSION_ID_KEY, mSessionId);
    map.put(SNAPZ_ID_KEY, mSnapzId);
    map.put(EMAIL_KEY, mEmail);
    map.put(EMAIL_PWD_KEY, mEmailPwd);

    JSONArray list = new JSONArray();
    list.add(map);
    jsonObject.put("parameters", list);
    jsonObject.put("function", "verifyEmail");

    return jsonObject;
}

但是,当我使用 lint checker 时,我不断收到此警告。

[unchecked] unchecked call to add(E) as a member of the raw type ArrayList
        list.add(map);
                ^
where E is a type-variable: E extends Object declared in class ArrayList

 warning: [unchecked] unchecked call to put(K,V) as a member of the raw type HashMap
        jsonObject.put("parameters", list);
                      ^
where K,V are type-variables:
    K extends Object declared in class HashMap
    V extends Object declared in class HashMap

warning: [unchecked] unchecked call to put(K,V) as a member of the raw type HashMap
        jsonObject.put("function", "verifyEmail");

我尝试使用泛型类型。 HashMap 使用泛型,但其他对象 JSONObject JSONArray 不使用。

非常感谢您的任何建议,

【问题讨论】:

标签: java json-simple


【解决方案1】:

您收到此警告是因为该库在内部使用原始类型集合。 要隐藏此警告,请使用 @SuppressWarnings("unchecked") 注释您的方法。

如果你想使用支持泛型的 json 库。你可以去Google GSOn。我自己在很多项目中都使用过它。它很简单并且使用通用集合。

【讨论】:

  • 所以答案“从不使用com.googlecode.json-simple”因为谁来注释代码中的所有内容?
猜你喜欢
  • 2021-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 2015-08-29
  • 2019-04-13
  • 1970-01-01
相关资源
最近更新 更多