简体   繁体   English

如何添加ArrayList <String> JSON数组-牢记类型安全

[英]How to add ArrayList<String> to JSON Array - keeping type safety in mind

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. Eclipse显示了类型安全警告,我几乎尝试了所有方法来消除它(当然可以选择取消它),但是不幸的是我没有成功。
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? 还是@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. 类型安全:方法addAll(Collection)属于原始类型ArrayList。 References to generic type ArrayList should be parametrized 泛型类型ArrayList的引用应参数化

JSONArray is from org.json.simple.JSONArray . JSONArray来自org.json.simple.JSONArray Would you recommend another package? 您会推荐其他套餐吗?

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 谢谢

org.json and org.json.simple JSON parser use raw types of collections underneath. org.json和org.json.simple JSON解析器使用下面的原始集合类型。 If you're looking for good Generics support try Google Gson . 如果您正在寻找良好的泛型支持,请尝试Google Gson Here's how you would go about serializing your generic ArrayList with Gson: 这是使用Gson序列化通用ArrayList

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 Generics intact: 这是在完整的泛型下反序列化相同JSON字符串的方法:

Type type = new TypeToken<ArrayList<String>>(){}.getType();
ArrayList<String> arrayList = gson.fromJson(json, type);

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方法放置的内容,这是因为如果未提供type,则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. 在不提供type参数的情况下,E退回到Object,这使addAll方法成为一种可以在现有集合的顶部添加包含任何内容的集合的方法。 Therefore, you can do something like this: 因此,您可以执行以下操作:

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. 狗和汽车被添加到同一JSONArray(ArrayList)中,并被视为对象。 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. 这就是警告存在的原因。

By using the generic type parameter (eg 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. 这样可以确保该参数只能接受Dog和Dog的子类的集合。 Therefore when you retrieve it from the collection, it is safe to assign the retrieve object to Dog. 因此,当您从集合中检索它时,将检索对象分配给Dog是安全的。

The warning is not because of you did something wrong. 警告不是因为您做错了什么。 It is because of the JSONArray inherits a non-parametrized Collection. 这是因为JSONArray继承了非参数化的Collection。 Feel free to suppress it. 随时抑制它。

From the documentation for JSONArray , it looks like it extends ArrayList the raw type (it doesn't have a generic type parameter). JSONArray的文档中,它似乎extends ArrayList的原始类型(它没有通用类型参数)。 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期望接收某些String ,然后禁止显示警告。

(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更新为使用泛型,但与此同时,您必须使用原始类型。)

Use Gson library to convert ArrayList to JsonArray . 使用Gson库将ArrayList转换为JsonArray

Gson gson = new GsonBuilder().create();
JsonArray myCustomArray = gson.toJsonTree(myCustomList).getAsJsonArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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