JSON and Generics in Java - Type safety warning

后端 未结 7 1440
逝去的感伤
逝去的感伤 2020-12-10 05:05

I have some data stored in Java elements and I need to return it in a given format - JSONObject. While my implementation works fine, I\'m still getting a warning message fro

7条回答
  •  孤街浪徒
    2020-12-10 05:20

    What is your JSONObject, does it inherit from HashMap? If does, the warn probably means that your should declare the JSONObject instance as follows:

    JSONObject obj=new JSONObject();
    

    Updated: Look at the definition of the JSONObject:

    public class JSONObject extends HashMap
    

    it extends HashMap but doesn't support parameter type, if its definition is

    public class JSONObject extends HashMap
    

    then we could write

    JSONObject obj=new JSONObject();
    

    and the put method will no longer generate the warning

提交回复
热议问题