如何添加的ArrayList&LT;串GT;以JSON阵列 - 牢记类型安全 [英] How to add ArrayList<String> to JSON Array - keeping type safety in mind
问题描述
Eclipse中显示的类型安全警告,我想几乎所有消灭它(当然SUP pressing这将是一个选项)但不幸的是我没有任何成功。结果
你知道我如何改变我的code,以便有没有类型安全的警告了。或者是 @燮pressWarnings(未登记)
的唯一途径?
Eclipse displayed a type safety warning and I tried nearly everything to eradicate it (of course suppressing it would be an option) but unfortunately I hadn't any success.
Do you know how I have to change my code so that there is no type safety warning anymore. Or is @SuppressWarnings("unchecked")
the only way?
ArrayList<String> arrayList= (ArrayList<String>) Classname.getArrayList();
JSONArray obj = new JSONArray();
obj.addAll(arrayList);
在以下类型的安全警示的最后一行显示:
in the last line following type safety warning is displayed:
类型的安全:
该方法的addAll(集合)属于原始类型的ArrayList。引用泛型类型的ArrayList应当参数化
Type safety: The method addAll(Collection) belongs to the raw type ArrayList. References to generic type ArrayList should be parametrized
JSONArray是 org.json.simple.JSONArray
。你会推荐另一包?
JSONArray is fromorg.json.simple.JSONArray
. Would you recommend another package?
推荐答案
如果你想使用JSON工作去这个库,这个库有一个很好的支持的 HTTPS://$c$c.google.com/p/google-gson/
If you want to work with json go this library, this library has a nice support https://code.google.com/p/google-gson/.
这是一个例子:
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
感谢
这篇关于如何添加的ArrayList&LT;串GT;以JSON阵列 - 牢记类型安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!