简体   繁体   中英

Why am I getting an Unchecked Assignment Warning?

I am getting this warning when using Gson to convert a JSON string to a Java object. Why am I getting it and how can I solve it?

This is the warning I getting in code:

Unchecked assignment: 
    'net.brawtasports.brawtasportsgps.model.JSONKeys' to 
    'net.brawtasports.brawtasportsgps.model.JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>'

This is the error I get at runtime:

java.lang.ClassCastException:
    com.google.gson.internal.LinkedTreeMap cannot be cast to
    net.brawtasports.brawtasportsgps.model.team.Object

And this is my code:

  String jsonString = Preferences.readFromPreferences(ApplicationConstants.team_data,"");
    Gson gson = new Gson();
    JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class); //converts json string to java object
    Object players = teamMembers.getObject();//object is my custom class
    //ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item,players);
    ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,players.getPlayersSummary());
    player1.setAdapter(arrayAdapter);

Here is the code for my JSONKeys POJO:

   public class JSONKeys<T> {

private boolean Success;
private String Message;
private int ObjectIdentifier;
private T Object;
private java.util.List<List> List = new ArrayList<List>();
private int TotalRecords;
private String ErrorMessage;
private int Code;


private net.brawtasports.brawtasportsgps.model.match.Criteria Criteria;

private net.brawtasports.brawtasportsgps.model.match.SearchInfo SearchInfo;
//Add Object from match class


/**
 * @return The Criteria
 */
public net.brawtasports.brawtasportsgps.model.match.Criteria getCriteria() {
    return Criteria;
}

/**
 * @param Criteria The Criteria
 */
public void setCriteria(net.brawtasports.brawtasportsgps.model.match.Criteria Criteria) {
    this.Criteria = Criteria;
}

/**
 * @return The SearchInfo
 */
public net.brawtasports.brawtasportsgps.model.match.SearchInfo getSearchInfo() {
    return SearchInfo;
}

/**
 * @param SearchInfo The SearchInfo
 */
public void setSearchInfo(net.brawtasports.brawtasportsgps.model.match.SearchInfo SearchInfo) {
    this.SearchInfo = SearchInfo;
}

/**
 * @return The List
 */
public java.util.List<List> getList() {
    return List;
}

/**
 * @param List The List
 */
public void setLArrayListList(java.util.List<List> List) {
    this.List = List;
}


/**
 * @return The Code
 */
public int getCode() {
    return Code;
}

/**
 * @param Code The Code
 */
public void setCode(int Code) {
    this.Code = Code;
}

/**
 * @return The TotalRecords
 */
public int getTotalRecords() {
    return TotalRecords;
}

/**
 * @param TotalRecords The TotalRecords
 */
public void setTotalRecords(int TotalRecords) {
    this.TotalRecords = TotalRecords;
}

/**
 * @return The ErrorMessage
 */
public String getErrorMessage() {
    return ErrorMessage;
}

/**
 * @param ErrorMessage The ErrorMessage
 */
public void setErrorMessage(String ErrorMessage) {
    this.ErrorMessage = ErrorMessage;
}

/**
 *
 * @return
 * The message
 */
public String getMessage() {
    return Message;
}

/**
 *
 * @param message
 * The message
 */
public void setMessage(String message) {
    this.Message = message;
}


/**
 *
 * @return
 * The Success
 */
public boolean isSuccess() {
    return Success;
}

/**
 *
 * @param Success
 * The Success
 */
public void setSuccess(boolean Success) {
    this.Success = Success;


}


/**
 *
 * @return
 * The ObjectIdentifier
 */
public int getObjectIdentifier() {
    return ObjectIdentifier;
}

/**
 *
 * @param ObjectIdentifier
 * The ObjectIdentifier
 */
public void setObjectIdentifier(int ObjectIdentifier) {
    this.ObjectIdentifier = ObjectIdentifier;
}

/**
 *
 * @return
 * The Object
 */
public T getObject() {
    return Object;
}

/**
 *
 * @param Object
 * The Object
 */
public void setObject(T Object) {
    this.Object = Object;
}

}

and for my Object POJO it is:

   public class Object {

private HomeTeamGoals HomeTeamGoals;
private AwayTeamGoals AwayTeamGoals;
private List<PlayersSummary> PlayersSummary = new ArrayList<PlayersSummary>();
private TeamSummary TeamSummary;

/**
 *
 * @return
 * The HomeTeamGoals
 */
public HomeTeamGoals getHomeTeamGoals() {
    return HomeTeamGoals;
}

/**
 *
 * @param HomeTeamGoals
 * The HomeTeamGoals
 */
public void setHomeTeamGoals(HomeTeamGoals HomeTeamGoals) {
    this.HomeTeamGoals = HomeTeamGoals;
}

/**
 *
 * @return
 * The AwayTeamGoals
 */
public AwayTeamGoals getAwayTeamGoals() {
    return AwayTeamGoals;
}

/**
 *
 * @param AwayTeamGoals
 * The AwayTeamGoals
 */
public void setAwayTeamGoals(AwayTeamGoals AwayTeamGoals) {
    this.AwayTeamGoals = AwayTeamGoals;
}

/**
 *
 * @return
 * The PlayersSummary
 */
public List<PlayersSummary> getPlayersSummary() {
    return PlayersSummary;
}

/**
 *
 * @param PlayersSummary
 * The PlayersSummary
 */
public void setPlayersSummary(List<PlayersSummary> PlayersSummary) {
    this.PlayersSummary = PlayersSummary;
}

/**
 *
 * @return
 * The TeamSummary
 */
public TeamSummary getTeamSummary() {
    return TeamSummary;
}

/**
 *
 * @param TeamSummary
 * The TeamSummary
 */
public void setTeamSummary(TeamSummary TeamSummary) {
    this.TeamSummary = TeamSummary;
}
 }

You need to use a fully specified type when deserializing generics. They key line that needs changing is here:

JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class);

Because you're using JSONKeys.class , Gson doesn't know what the generic type of JSONKeys is. Instead, use this:

JSONKeys<Object> teamMembers = gson.fromJson(jsonString, 
        new TypeToken<JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>>(){}.getType());

This will tell Gson which generic type you need to use. See my explanation for why this works here: How does Gson TypeToken work?

As an aside, don't name your classes the same thing as anything in the java.lang package. It makes your code really confusing for people reading it and can cause unforseen bugs when you accidentally refer to the wrong one by forgetting an import statement.

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