Gson数据存储和检索。存储为字符串,检索为整数

时间:2016-08-25 02:39:37

标签: java arrays string gson parseint

在我的datastorage类中,我使用Gson保存名为setTeam的String数据并检索名为getTeam的数据

public class DataStorage {
        private static final String TEAM = "Teams";
        private static final String TEAM_INFO = "teaminfo";
        private static Gson gson = new Gson();


        public static void setTeam(MainActivity context, Collection<User> teams) {
            String usersJson = gson.toJson(teams);
            SharedPreferences.Editor editor = context.getSharedPreferences(TEAM_INFO, Context.MODE_PRIVATE).edit();
            editor.putString(TEAM, usersJson);
            editor.apply();
        }

        public static ArrayList<User> getTeam(Context context){
            SharedPreferences prefs = context.getSharedPreferences(TEAM_INFO, Context.MODE_PRIVATE);
            String usersJson = prefs.getString(TEAM, "[]");
            return gson.fromJson(usersJson, new TypeToken<ArrayList<User>>(){}.getType());
        }

    }

然后我为每个保存在json数据库中的团队创建了一个名为User的类。因此,对于每个setTeam,都有一个teamName gameWongameLost

public class User implements Serializable {

    private String teamName;
    private int gameWon;
    private int gameLost;

    public void setName(String name) {this.teamName = name;}
    public String getName() {return teamName;}

    public void setGameWon(int wonHome){this.gameWon = wonHome;}
    public int getGameWon(){return gameWon;}

    public void setGameLost (int gameLoss){this.gameLost = gameLoss;}
    public int getGameLost(){return gameLost;}
}

在主要活动中,我创建了一个包含一些数字的双字符串ArrayList

private final String[][] dataTeams = new String[30][3];

所以在dataTeams double Array中有30个团队使用他们的gameWon和gameLost。 gameWon和gameLost是数字字符串。

所以现在我试图将dataTeams中的任何内容保存到Json数据库中。

User c = new User();
c.setName(dataTeams[i][0]);
c.setGameWon(Integer.parseInt(dataTeams[i][2]));
c.setGameLost(Integer.parseInt(dataTeams[i][3]));

teams.add(c);
DataStorage.setTeam(this, teams);

我成功完成了,但是当我检索数据时;

ArrayList<User> users = DataStorage.getTeam(this);
        String [] teams = new String[30];
        for(int i = 0; i < 30; i++) {
            teams[i] = users.get(i).getName() + "  W:" + users.get(i).getGameWon() + " L:" + users.get(i).getGameLost();
        }

getName对所有团队都是正确的,但是他们的getGameWon和getGameLost都是0,而不是double数组中的dataTeams

所以现在我陷入困境,当我解析为int时,我所做的Json类DataStorage是否没有保存字符串

c.setGameWon(Integer.parseInt(dataTeams[i][2]));
c.setGameLost(Integer.parseInt(dataTeams[i][3]));

或者我正在解析整数错误。

dataTeam中的数据是

Team    W   L   
Atlanta     60  22  
Boston      40  42  
Brooklyn    38  44  
Charlotte   33  49
Chicago     50  32  
Cleveland   53  29  
Dallas      50  32  
Denver      30  52  
Detroit     32  50  
Golden State 67 15  
Houston     56  26  
Indiana     38  44  
L.A. Clippers   56  26  
L.A. Lakers     21  61  
Memphis     55  27  
Miami       37  45  
Milwaukee   41  41  
Minnesota   16  66  
New Orleans 45  37  
New York    17  65  
Oklahoma City   45  37  
Orlando     25  57  
Philadelphia    18  64  
Phoenix     39  43  
Portland    51  31  
Sacramento  29  53  
San Antonio 55  27  
Toronto     49  33  
Utah        38  44  
Washington  46  36  

1 个答案:

答案 0 :(得分:0)

您的声明:

private final String[][] dataTeams = new String[30][3];

您的设置数据逻辑:

c.setGameWon(Integer.parseInt(dataTeams[i][2]));  // << [i][1]
c.setGameLost(Integer.parseInt(dataTeams[i][3]));  // << [i][2]

可能是索引不正确导致错误发生。