gpt4 book ai didi

java - 为什么我会收到未经检查的作业警告?

转载 作者:行者123 更新时间:2023-12-01 18:12:37 25 4
gpt4 key购买 nike

当我使用 Gson 将 JSON 字符串转换为 Java 对象时收到此警告。为什么我会收到它以及如何解决它?

这是我在代码中收到的警告:

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

这是我在运行时遇到的错误:

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

这是我的代码:

  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);

这是我的 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;
}

}

对于我的 Object POJO 来说是:

   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;
}
}

最佳答案

反序列化泛型时需要使用完全指定的类型。他们需要更改的关键行在这里:

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

因为您使用的是 JSONKeys.class,Gson 不知道 JSONKeys 的通用类型是什么。相反,使用这个:

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

这将告诉 Gson 您需要使用哪种泛型类型。请参阅我的解释,了解其为何有效:How does Gson TypeToken work?

顺便说一句,不要将类命名为与 java.lang 包中的任何名称相同的名称。它使您的代码对于阅读它的人来说确实令人困惑,并且当您因忘记 import 语句而意外引用错误的代码时,可能会导致不可预见的错误。

关于java - 为什么我会收到未经检查的作业警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com