简体   繁体   中英

Java/Android - Gson().fromJson() with Type is still giving an Unchecked cast Warning

I use the Gson().fromJson method to convert a JSON-String to a List of my Object (Products). I've heard I need to use a Type as a parameter in the fromJson to prevent getting an Unchecked cast Warning, but I'm still getting it..

Here is my code:

List<Product> p = null;
try{
    // Convert JSON-string to a List of Product objects
    Type listType = new TypeToken<List<Product>>(){}.getType();
    p = (List<Product>)new Gson().fromJson(json, listType);
}
catch(JsonParseException ex){
    ex.printStackTrace();
}

Why do I still get the Unchecked cast Warning [Object to List]?

I changed

List<Product> p = (List<Product>) new Gson().fromJson(json, listType);

to

List<Product> p = new Gson().fromJson(json, listType);

and now I don't have the warning anymore.

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