5

我有以下使用 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不使用。

非常感谢您的任何建议,

4

1 回答 1

17

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

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

于 2014-07-03T09:13:20.010 回答