Java 如何将 ArrayList<String> 添加到 JSON 数组 - 牢记类型安全
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18335214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add ArrayList<String> to JSON Array - keeping type safety in mind
提问by Brenne
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?
Eclipse 显示了一个类型安全警告,我尝试了几乎所有方法来消除它(当然抑制它是一种选择)但不幸的是我没有任何成功。
你知道我必须如何更改我的代码,以便不再有类型安全警告。还是@SuppressWarnings("unchecked")
唯一的办法?
ArrayList<String> arrayList= (ArrayList<String>) Classname.getArrayList();
JSONArray obj = new JSONArray();
obj.addAll(arrayList);
in the last line following type safety warning is displayed:
在最后一行显示以下类型安全警告:
Type safety: The method addAll(Collection) belongs to the raw type ArrayList. References to generic type ArrayList should be parametrized
类型安全:方法 addAll(Collection) 属于原始类型 ArrayList。对泛型类型 ArrayList 的引用应该被参数化
JSONArray is fromorg.json.simple.JSONArray
. Would you recommend another package?
JSONArray 来自org.json.simple.JSONArray
. 你会推荐另一个包吗?
采纳答案by Felquir
If you want to work with json go this library, this library has a nice support https://code.google.com/p/google-gson/.
如果你想使用 json 去这个库,这个库有一个很好的支持https://code.google.com/p/google-gson/。
This is an example:
这是一个例子:
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]
Thanks
谢谢
回答by chrylis -cautiouslyoptimistic-
From the documentation for JSONArray
, it looks like it extends ArrayList
the raw type (it doesn't have a generic type parameter). This is an example of dealing with non-generic legacy code, and the only way to proceed is to make very sure that the JSONArray
is expecting to be receiving some String
s and then to suppress the warning.
从 的文档来看JSONArray
,它看起来像是extends ArrayList
原始类型(它没有泛型类型参数)。这是一个处理非通用遗留代码的例子,唯一的方法是确保JSONArray
预期会收到一些String
s,然后抑制警告。
(The real way to fix it would be to update JSONArray
to use generics, but in the meantime, you have to use the raw type.)
(修复它的真正方法是更新JSONArray
以使用泛型,但与此同时,您必须使用原始类型。)
回答by Ravi Thapliyal
org.json and org.json.simple JSON parser use raw types of collections underneath. If you're looking for good Genericssupport try Google Gson. Here's how you would go about serializing your generic ArrayList
with Gson:
org.json 和 org.json.simple JSON 解析器在下面使用原始类型的集合。如果您正在寻找良好的泛型支持,请尝试Google Gson。以下是ArrayList
使用 Gson序列化泛型的方法:
Gson gson = new Gson();
ArrayList<String> arrayList= (ArrayList<String>) ClassName.getArrayList();
// Serializing to a JSON element node
JsonElement jsonElement = gson.toJsonTree(arrayList);
System.out.println(jsonElement.isJsonArray()); // true
// Or, directly to JSON string
String json = gson.toJson(arrayList);
System.out.println(json);
Here's how you would deserialize the same JSON string with its Genericsintact:
以下是如何反序列化相同的 JSON 字符串并使其泛型完好无损:
Type type = new TypeToken<ArrayList<String>>(){}.getType();
ArrayList<String> arrayList = gson.fromJson(json, type);
回答by KKKCoder
The short answer for your question is that you have to suppress it in order for it to go away. The problem is not about what you put using the addAll method, it is because of the JSONArray has no way to guarantee type safety if type is not provided.
你的问题的简短回答是你必须压制它才能让它消失。问题不在于您使用 addAll 方法放置了什么,而是因为如果未提供类型,则 JSONArray 无法保证类型安全。
JSONArray inherits a non-parametrized ArrayList and the addAll method is defined as:
JSONArray 继承了一个非参数化的 ArrayList 并且 addAll 方法定义为:
public boolean addAll(java.util.Collection<? extends E> es)
Without providing the type parameter, E falls back to Object, which makes the addAll method a method that can add a collection that contains ANYTHING on top of the existing collections. Therefore, you can do something like this:
在不提供类型参数的情况下,E 回退到 Object,这使得 addAll 方法成为一种可以在现有集合之上添加包含 ANYTHING 的集合的方法。因此,您可以执行以下操作:
List<Dog> dogs = new ArrayList<Dog>();
dogs.add(new Chiwawa());
List<Car> cars = new ArrayList<Car>();
cars.add(new BMW());
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(dogs);
jsonArray.addAll(cars);
Dogs and Cars are added together to the same JSONArray (ArrayList) and treated as barely Object. If you do something like this, when you retrieve any of the object back, you have no way to tell whether it is a dog or a car. This is why the warning exists.
Dogs 和 Cars 被一起添加到同一个 JSONArray (ArrayList) 并被视为几乎没有对象。如果你做这样的事情,当你取回任何物体时,你无法分辨它是狗还是车。这就是警告存在的原因。
By using the generic type parameter (e.g. Dog), the addAll definition will be like:
通过使用泛型类型参数(例如 Dog),addAll 定义将类似于:
public boolean addAll(java.util.Collection<? extends Dog> es)
This make sure the parameter can only accept a collection of Dog and Dog's child class. Therefore when you retrieve it from the collection, it is safe to assign the retrieve object to Dog.
这确保参数只能接受 Dog 和 Dog 的子类的集合。因此,当您从集合中检索它时,将检索对象分配给 Dog 是安全的。
The warning is not because of you did something wrong. It is because of the JSONArray inherits a non-parametrized Collection. Feel free to suppress it.
警告不是因为你做错了什么。这是因为 JSONArray 继承了一个非参数化的 Collection。随意压制它。