简体   繁体   中英

How to get rid of type warning when using JSONArray and JSONObject

I want to create a JSON string piece by piece and by using org.json.simple.JSONArray and org.json.simple.JSONObject. Here's the code.

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

There is Eclipse warning for line 2 and 3:

  • type safety: the method add(Object) belongs to the raw type ArrayList. references to generic type should be parameterized.

And warning for line 5 and 6:

  • type safety: the method put(object, object) belongs to the raw type HashMap. references to generic type HashMap should be parameterized.

How can I get rid of these warnings?

PS

The problem addressed here is different from this How to correctly use HashMap? because there's no way to create a JSONArray<String> or JSONObject<String> . JSONArray and JSONObject are not parameterizable.

It appears that there's no way to parameterize org.json.simple.JSONArray and org.json.simple.JSONObject by its design. However, you can suppress these warnings by annotation:

@SuppressWarnings("unchecked")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM