JSON and Generics in Java - Type safety warning

后端 未结 7 1427
逝去的感伤
逝去的感伤 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:17

    You could create a map object and then do an explicit cast to JSONObject

    Map obj =  new HashMap();
    obj.put("id",way.getId());
    JSONObject jsonObj =  (JSONObject) obj;
    

    But note that this will restrict you only include "Strings" in your JSON. and you will see compile errors if you put another data structure. Say an array.

提交回复
热议问题