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

Java 为什么我会收到未经检查的作业警告?,java,android,json,gson,classcastexception,Java,Android,Json,Gson,Classcastexception,我在使用Gson将JSON字符串转换为Java对象时收到此警告。为什么我会得到它,我如何才能解决它 这是我在代码中得到的警告: Unchecked assignment: 'net.brawtasports.brawtasportsgps.model.JSONKeys' to 'net.brawtasports.brawtasportsgps.model.JSONKeys<net.brawtasports.brawtasportsgps.model.team.Objec

我在使用Gson将JSON字符串转换为Java对象时收到此警告。为什么我会得到它,我如何才能解决它

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

Unchecked assignment: 
    'net.brawtasports.brawtasportsgps.model.JSONKeys' to 
    'net.brawtasports.brawtasportsgps.model.JSONKeys<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);
String jsonString=Preferences.readFromPreferences(ApplicationConstants.team_data,”);
Gson Gson=新的Gson();
JSONKeys teamMembers=gson.fromJson(jsonString,JSONKeys.class)//将json字符串转换为java对象
对象players=teamMembers.getObject()//对象是我的自定义类
//ArrayAdapter ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple\u spinner\u dropdown\u项目,玩家);
ArrayAdapter ArrayAdapter=新的ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item,players.getPlayerSummary());
player1.setAdapter(阵列适配器);
以下是我的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;
}
公共类JSONKeys{
私人布尔成功;
私有字符串消息;
私人识别器;
私人T对象;
private java.util.List List=new ArrayList();
私人档案;
私有字符串错误消息;
私有整数码;
private net.brawtasports.brawtasportsgps.model.match.Criteria;
private net.brawtasports.brawtasportsgps.model.match.SearchInfo SearchInfo;
//从匹配类添加对象
/**
*@返回标准
*/
public net.brawtasports.brawtasportsgps.model.match.Criteria getCriteria()的{
退货标准;
}
/**
*@param-Criteria-The-Criteria
*/
公共无效设置标准(net.brawtasports.brawtasportsgps.model.match.Criteria标准){
这个。标准=标准;
}
/**
*@返回搜索信息
*/
public net.brawtasports.brawtasportsgps.model.match.SearchInfo getSearchInfo(){
返回搜索信息;
}
/**
*@param SearchInfo搜索信息
*/
公共无效设置搜索信息(net.brawtasports.brawtasportsgps.model.match.SearchInfo搜索信息){
this.SearchInfo=SearchInfo;
}
/**
*@返回列表
*/
public java.util.List getList()文件{
退货清单;
}
/**
*@param列出列表
*/
public void setLArrayListList(java.util.List列表){
this.List=List;
}
/**
*@返回代码
*/
公共int getCode(){
返回码;
}
/**
*@param编码该代码
*/
公共无效设置码(整数码){
这个。代码=代码;
}
/**
*@返回所有记录
*/
public int getTotalRecords(){
返回所有记录;
}
/**
*@param TotalRecords TotalRecords
*/
公共无效setTotalRecords(整数TotalRecords){
this.TotalRecords=TotalRecords;
}
/**
*@返回错误消息
*/
公共字符串getErrorMessage(){
返回错误消息;
}
/**
*@param ErrorMessage错误消息
*/
public void setErrorMessage(字符串ErrorMessage){
this.ErrorMessage=ErrorMessage;
}
/**
*
*@返回
*信息
*/
公共字符串getMessage(){
返回消息;
}
/**
*
*@param消息
*信息
*/
公共无效设置消息(字符串消息){
this.Message=消息;
}
/**
*
*@返回
*成功
*/
公共布尔值isSuccess(){
回归成功;
}
/**
*
*@param成功
*成功
*/
public void setSuccess(布尔值成功){
成功=成功;
}
/**
*
*@返回
*目标识别器
*/
public int getObjectIdentifier(){
返回对象标识符;
}
/**
*
*@param ObjectIdentifier
*目标识别器
*/
公共无效setObjectIdentifier(int ObjectIdentifier){
this.ObjectIdentifier=ObjectIdentifier;
}
/**
*
*@返回
*对象
*/
公共T getObject(){
返回对象;
}
/**
*
*@param对象
*对象
*/
公共无效集合对象(T对象){
this.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;
}
 }
公共类对象{
私人家庭团队目标家庭团队目标;
私人AwayTeamGoals AwayTeamGoals;
private List PlayersSummary=new ArrayList();
私人团队总结团队总结;
/**
*
*@返回
*家庭队目标
*/
公共HomeTeamGoals getHomeTeamGoals(){
返回家庭目标;
}
/**
*
*@param HomeTeamGoals
*家庭队目标
*/
公共无效设置HomeTeamGoals(HomeTeamGoals HomeTeamGoals){
this.HomeTeamGoals=HomeTeamGoals;
}
/**
*
*@返回
*AwayTeamGoals
*/
公共AwayTeamGoals getAwayTeamGoals(){
返回目标;
}
/**
*
*@param AwayTeamGoals
*AwayTeamGoals
*/
公共无效SetAwyteAmGoals(AwyteAmGoals AwyteAmGoals){
this.AwayTeamGoals=AwayTeamGoals;
}
/**
*
*@返回
*球员摘要
*/
公共列表GetPlayerSummary(){
返回玩家摘要;
}
/**
*
*@param PlayersSummary
*球员摘要
*/
public void setPlayersSummary(列表PlayersSummary){
this.PlayersSummary=PlayersSummary;
}
/**
*
*@返回
*团队总结
*/
public TeamSummary getTeamSummary(){
返回汇总表;
}
/**
*
*@param-TeamSummary
*团队总结
*/
公共无效设置摘要(团队摘要团队摘要){
this.TeamSummary=TeamSummary;
}
}

反序列化泛型时,需要使用完全指定的类型。他们需要改变的关键是:

JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class);
这将告诉Gson您需要使用哪种泛型类型。请参见我的解释,了解其工作原理:


另外,不要将类命名为与
java.lang
包中的任何东西相同的名称。它会让阅读你的代码的人感到困惑,如果你忘记了
import
语句而意外引用了错误的代码,可能会导致无法预见的错误。

你能显示JSONKeys的代码吗?我已经更新了帖子
JSONKeys<Object> teamMembers = gson.fromJson(jsonString, JSONKeys.class);
JSONKeys<Object> teamMembers = gson.fromJson(jsonString, 
        new TypeToken<JSONKeys<net.brawtasports.brawtasportsgps.model.team.Object>>(){}.getType());